1. Differences between postmessage and sendmessage Functions
You can use the postmessage and sendmessage functions to send custom messages. The difference is:
Postmessage only puts the message into the queue. no matter whether the message processing program processes the message, it returns the message and continues the execution. This is an asynchronous message serving function;
Sendmessage must be returned after the message processing program processes the message and continues to be executed. This is a synchronous message serving function;
In addition, the return value of postmessage indicates whether the postmessage function is correctly executed, and the return value of sendmessage indicates the return value after other programs process messages.
2. Customize the message sending Method
(1) Add the following code to define a message in the resource. h or stdax. h file:
# Define wm_my_message wm_user + 1
(2) Add the following code to the header file of the class where the message processing function is located:
// {Afx_msg (c ...)
Afx_msg ........
Afx_msg ........
Afx_msg void onmymessage (/* wparam, lparam */);//Whether the parameter depends on the actual situation
//} Afx_msg
Declare_message_map ()
(3) Add the following code to the CPP file of the class where the message processing function is located:
Begin_message_map (...,...)
// {Afx_msg_map (cpostmessageview)
On_command .......
On_message (wm_my_message, onmymessage)
//} Afx_msg_map
......
End_message_map ()
(4) manually add the corresponding message function code to the CPP file of the class where the message processing function is located:
Void cpostmessageview: onmymessage (/* wparam, lparam */)
{
........
........
}
(5) Add the Code where the message needs to be sent:
Postmessage (wm_my_message );
Or
Sendmessage (wm_my_message );
3. Send custom messages from a class to the mainframe class
Directly Using the postmessage or sendmessage function can only send messages to the class where the function is located. To send messages to the mainframe class, you can use the following code:
(Cmainframe *) afxgetmainwnd ()-> postmessage (wm_xxx );
Or
(Cmainframe *) afxgetmainwnd ()-> sendmessage (wm_xxx );
Reprinted statement: This article from http://wmnmtm.blog.163.com/blog/static/3824571420097510938616/