Home>>Programming>> » How to Write Action Components and Register Action Components

How to Write Action Components and Register Action Components

Last Updated on August 5, 2018 By hcyaobin

How to write action components?

You can also create your own predefined action classes. When you write your own action classes, you can build in the ability to execute on certain target classes of object. Then, you can use your custom actions in the same way you use pre-defined action classes. That is, when the action can recognize and apply itself to a target class, you can simply assign the action to a client control, and it acts on the target with no need to write an event handler.

Component writers can use the classes in the QStdActns and DBActns units as examples for deriving their own action classes to implement behaviors specific to certain controls or components. The base classes for these specialized actions (TEditAction, TWindowAction, and so on) generally override HandlesTarget, UpdateTarget, and other methods to limit the target for the action to a specific class of objects. The descendant classes typically override ExecuteTarget to perform a specialized task. These methods are described here:

Method Description
HandlesTarget Called automatically when the user invokes an object (such as a tool button or menu item) that is linked to the action. The HandlesTarget method lets the action object indicate whether it is appropriate to execute at this time with the object specified by the Target parameter as a “Target” See How actions find their targets for details.
UpdateTarget Called automatically when the application is idle so that actions can update themselves according to current conditions. Use in place of OnUpdateAction. See Updating actions for details.
ExecuteTarget Called automatically when the action fires in response to a user action in place of OnExecute (for example, when the user selects a menu item or presses a tool button that is linked to this action). See What happens when an action fires for details.

When you write your own action classes, it is important to understand the following:

  1. How actions find their targets
  2. Registering actions

How to register actions

When you write your own actions, you can register actions to enable them to appear in the Action List editor. You register and unregister actions by using the global routines in the Actnlist unit:

extern PACKAGE void __fastcall RegisterActions(const AnsiString CategoryName, TMetaClass* const* AClasses, const int AClasses_Size, TMetaClass* Resource);
extern PACKAGE void __fastcall UnRegisterActions(TMetaClass* const * AClasses, const int AClasses_Size);

When you call RegisterActions, the actions you register appear in the Action List editor for use by your applications. You can supply a category name to organize your actions, as well as a Resource parameter that lets you supply default property values.
For example, the following code registers actions with the IDE in the MyAction unit:

namespace MyAction
{
void __fastcall PACKAGE Register()
{
// code goes here to register any components and editors
TMetaClass classes[2] = {__classid(TMyAction1), __classid(TMyAction2)};
RegisterActions(“MySpecialActions”, classes, 1, NULL);
}
}

When you call UnRegisterActions, the actions no longer appear in the Action List editor.

Add a Comment

Your email address will not be published. Required fields are marked *