Defining events in C + + Builder

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.