![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
Q: Send and respond to user defined messages.Answer:If you don't know how to respond to events, see the FAQ titled Respond to messages sent to the application. Step 1: Add a #define for a user message ID to the header file of the class that needs to respond to the message. #include <vcl\Classes.hpp> #include <vcl\Controls.hpp> #include <vcl\StdCtrls.hpp> #include <vcl\Forms.hpp> // user messages are WM_USER + some value #define UM_SPECIALMESSAGE (WM_USER + 1001) Step 2: Stay in the same header file and add a message map and a function declaration to the class that will receive the custom message. private: // User declarations void __fastcall UMSpecialMessage(TMessage &Message); public: // User declarations __fastcall TForm1(TComponent* Owner); BEGIN_MESSAGE_MAP MESSAGE_HANDLER(UM_SPECIALMESSAGE,TMessage,UMSpecialMessage) END_MESSAGE_MAP(TForm) Step 3: Code the response function. This function just beeps to let you know that it ran. void __fastcall TForm1::UMSpecialMessage(TMessage &Message) { MessageBeep(MB_ICONEXCLAMATION); } Step 4: Somebody needs to send a message. You would do this using SendMessage, PostMessage, or Perform. // SendMessage and Perform send the message directly to the window, and // the handler will run before the call returns. PostMessage adds the // message to the window's message queue and won't be handled until the // application fetches the message from the queue. SendMessage(Form1->Handle, UM_SPECIALMESSAGE, /*WPARAM*/ 0, /*LPARAM*/ 0); PostMessage(Form1->Handle, UM_SPECIALMESSAGE, /*WPARAM*/ 0, /*LPARAM*/ 0); Perform(UM_SPECIALMESSAGE, /* WPARAM */ 0, /* LPARAM */ 0); | ||||||
All rights reserved. |