1. Create a class declaration for a component
Class PACKAGE New classes Name: Public ancestor class name
{
Private: Private Data:
(1) Internal private Use
(2) Publish property's real Data Save
(3) Publish event data Save
(1,2,3) Private data initialization is generally in the constructor, (3) initialization is generally connected to the ancestor of the event. The
is usually used to facilitate direct invocation of ancestor methods, which often save ancestor-type variables: the TypeDef ancestor class name Inherit;
Private method: Used only within the class definition, does not want to be inherited, and does not want to be used publicly. (as far as possible to write a common function)
Protected: Protecting data: generally rarely used.
Protection method: typically a virtual function. Used internally during class definition, objects cannot be called directly, and the derived class can be controlled openly or not by their deriving classes, which may be redefined and exposed.
Public: Common data: General properties, but not publications (that is, the design period cannot be manipulated) properties that are not true data require private data to hold properties, and action attributes are passed by methods. Commonly used definitions such as:
Mode 1:
__property ansistring itemdata={read=fitemdata,write=fitemdata,default= "A"},
Mode 2:
__ Property ansistring itemdata={read=getitemdata,write=setitemdata,default= "A"};
Fitemdata is its data private storage getitemdata (), SetItemData () is the implementation of the read-write function, its internal in addition to the setting of data can also add corresponding processing code, this part of the processing is the greatest advantage of attributes. That is, by directly modifying the property to achieve both read and write data and the implementation of the code processing, but the user's feeling seems to have only made the data changes, it is this "side effects" brought.
In addition, if a modification to a property does not require response processing, then only the definition of 1 is OK.
Public method: Implements the encapsulation of the class, changing the data members through the public function. (method) redefining a function usually calls the ancestor class first inherit::xxx ();(inherit has explained, XXX table function name), and then write the processing code that you want to do.
__published: Publication properties: Properties of the same public part, except that the publication property can be modified directly from the object observer at design time. The Ancestor class has published properties, preferably here for confirmation.
Incidentally, the method should not be written at the publication Declaration, properties can also be written publicly or privately, but they cannot be accessed at design time, private properties are used internally onlyprovided by the user.
}
Write components should use attributes as much as possible, rather than using methods. Only work that the attribute cannot complete is done by the method.
To publish an event:
1. Ordinary event: Parameter only has tobject *sender, general use Tnotifyevent (pointer type)
such as: __property tnotifyevent onexit={read=fonexit,write=fonexit};
Private data is required to hold event pointers as well as properties.
Tnotifyevent Fonexit In addition to event handling code.
void __fastcall myonexit (system::tobject *sender) The event is triggered by the Fonexit (Sender) statement in its implementation code (note that the event-handling function has a return value of void)
2. Special Events:
First: You need to define an event type pointer (similar to the definition of a function pointer)
typedef void __fastcall (__closure *tkeydownevent) (system::tobject* Sender, WORD &key,tshiftstate Shift);
Second: There must be a data storage place. In the private place declared: such as Tkeydownevent Fonkeydown;
Again: The corresponding event handler function is generally declared at the protected. Such as
protected:virtual void __fastcall Myonkeydown (system::tobject *sender, WORD &key,tshiftstate Shift);
The code often fonkeydown (Sender,key,shift)
Finally: Set up the connection (need to provide the first three: event pointer type, data store variable, event handler function).
__property tkeydownevent Onkeydown={read=fonkeydown,write=fonkeydown}; };
Component inspection and registration static inline void Validctrcheck (new class name *) {new class name (NULL);}
Make sure that there are no pure virtual functions in the control declaration because the virtual base class is not able to generate the instance.
namespace name space
{
void __fastcall PACKAGE Register ()
{
Tcomponentclass Classes[1] = {__classid (new class name)};
}
Registercomponents ("Owning package name", classes, 0);
You can also add the property editor to Registerpropertyeditor (...)}} Namespaces are a C + + attribute that allows you to differentiate between other controls. This name must conform to the specification, which is related to the file name of the control. If the Tmybutton is in Mybutton.cpp, then this namespace is mybutton. Generate new components using the New Component dialog box, regardless of the problem.