In C + + Builder, an event is a delegate model, which is the encapsulation of a message. If you have used VC, you know that there is no event in VC, and only the message processing function, and in C + + Builder is the event handler function to respond to the message. Likewise, the event itself is a pointer, a closure, usually in C + + Builder with two types of events: Notification type events (that is, tnotifyevent, encapsulation of Windows messages), and custom events. In addition, we also know that the event is ignited by a virtual function, such as OnExit event is ignited by the doexit virtual function, I write a custom event, it is obvious that the following events in the code I write will be the encapsulation of the wm_mymessage message.
. h File
#include <....>
.....
#define Wm_mymessage wm_user+100
typedef void __fastcall (__closure *tmyevent) (TObject *sender,param1,param2,......);
Class Tmycontrol:public Twincontrol
{
Private
Tmyevent fonmyevent;//saves a pointer to an event.
void __fastcall dosomething (tmessage &message);
Public
Begin_message_map
Vcl_message_handler (wm_mymessage,tmessage,dosomething);
End_message_map (Tcontrol);
Protected
virtual void __fastcall domyevent (Param1,........); /By this virtual function to trigger events
virtual void __fastcall WndProc (tmessage &message);
__published:
.........
__property tmyevent onmyevent={read=fonmyevent,write=fonmyevent};
.....
};
. cpp File
Omiting Constructor and Deconstructor
Virtual Function,which'll spring the event:tmyevent
void __fastcall Tmycontrol::D omyevent (Param1,.....)
{
if (fonmyevent)
{
Fonmyevent (this,param1,param2,..... PARAMN);
}
}
Message Handler
void __fastcall Tmycontrol::D osomething
{
Todo:add your code here ....
}
void __fastcall Tmycontrol::wndproc (tmessage &message)
{
if (message.msg==wm_mymessage)
{
Domyevent (Message.wparam,.....);
}
......
}
According to the code above, we will see a Onmyevent event in Object Inspector, just like any other event, the user can respond to the message by writing the code here and give the event trigger behavior according to the requirements of the application, and the code above is written. I ask you to adjust yourself.