To customize a message, follow these steps:
1. Define a message.
- # Define wm_mymessage (wm_user+ 100)
Microsoft recommends that you customize the message at least wm_user + 100, because many new controls also use wm_user messages.
2. Declare the message processing function in the afx_msg block in the. h file
- Afx_msg lresult onmymessage (wparam, lparam );
As follows:
- Protected:
- // {Afx_msg (...)
- Afx_msg int oncreate (maid );
- Afx_msg void ontimer (uint nidevent );
- Afx_msg lresult onmymessage (wparam, lparam );
- //} Afx_msg
- Declare_message_map ()
3. Add a message ing macro to the. cpp file.
- On_message (wm_my_message, onmymessage)
As follows:
- Begin_message_map (...)
- // {Afx_msg_map (...)
- On_wm_create ()
- On_wm_timer ()
- On_message (wm_mymessage, onmymessage)
- //} Afx_msg_map
- End_message_map ()
4. Edit the message processing function in the. cpp File
- Lresult yourclass: onmymessage (wparam, lparam)
- {
- Return 0;
- }
Message Interception Method
Use sendmessage (wm_mymessage) or postmessage (wm_mymessage) to send messages to the Message Queue where you need to use the message processing function.
The differences between the two are as follows:
Sendmessage: After a message is sent to the message queue, it is returned only after the message is processed. continue to run the code.
Postmessage: After a message is sent to the message queue, the message is returned immediately without waiting for message processing, and the code is executed.