Define events in C ++ Builder

zhaozj2021-02-16  43

In C Builder, event is a commission model that is a package of messages. If you have used VC, you know what event does not exist in the VC, and only the message handler, and in C Builder is responsible for responding to the event. The same is that the event itself is also a pointer, which is a closed bag, usually there are two events in C Builder: notify the type event (ie TNOTIFYEVENT, package packages for Windows messages) and custom events. In addition, we also know that the event is ignited through a virtual function. For example, the OneXIT event is ignited by the DoExit virtual function. Here I write a custom event, it is clear that the incident in the code I have written will be Package for WM_MYMESSAGE message ..h file # include <....> ..... # define wm_mymessage wm_user 100typedef void __fastcall (__closure * tmyevent) (Tobject * Sender, param1, param2, ...... );

class TMyControl: public TWinControl {private: TMyEvent FOnMyEvent; // save the pointing event pointer 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, .......); // is triggered by this virtual function to trigger the event Virtual void __fastcall wndproc (tMessage & Message); __ public: ......... __property tmyevent onmyevent = {read = fonmyEvent , WRITE = fonmyEvent} ;;;;;

.cpp file

// Omiting constructor and deconstructor // Virtual function, which will spring the event: TMyEventvoid __fastcall TMyControl :: DoMyEvent (Param1, .....) {if (FOnMyEvent) {FOnMyEvent (this, Param1, Param2, .... .Paramn);}} // Message Handlervoid __fastcall TMyControl :: DoSomething {// TODO: Add your code here ....} void __fastcall TMyControl :: WndProc (TMessage & Message) {if (Message.Msg == WM_MYMESSAGE) { Domyevent (Message.wParam, .....);} ...} According to the code, we will see an OnMyEvent event in the Object Inspector, just like other events, users write code To here, you can respond to the message and give the behavior of this event based on the application's needs. The above code is written, please adjust itself.

转载请注明原文地址:https://www.9cbs.com/read-27098.html

New Post(0)