Message transfer between two dialogs in MFC

In MFC projects sometimes it involves the transfer of messages between two dialog boxes. Here you need to use the SendMessage() function and the PostMessage() function.

The difference between these two functions can be determined by Baidu. Here I am using the SendMessage() function.

》The message transmission between the two dialog boxes is divided into two cases: 1. The sub-dialog box sends messages to the main dialog box; 2. The message is sent between the two sub-dialog boxes.

First introduction: 1" The sub-dialog sends a message to the main dialog

The child dialog "Dialog" sends a message to the main dialog "Message Send".


First, add #define WM_MyMessage WM_USER+100 to the common header file .h of the sub-dialog and main dialog to set an ID for the message.

Next, write the code in the sub-dialog where the message is to be sent::SendMessage(this->GetParent()->m_hWnd,WM_MyMessage,0,0); if there is content to be transmitted, it can be transmitted through the third and fourth parameters.


Then add the message receiving function in the header file of the main dialog: afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam);//The message function added by yourself


Then write the code in the .cpp file of the main dialog box, and the location of writing is as shown below: ON_MESSAGE(WM_MyMessage, &C message sending Dlg::OnMyMessage). The parameters: "WM_MyMessage" is the macro we defined in the public header file before; "C message sending Dlg" is the class of my main dialog; "OnMyMessage" is the message function we defined in the above figure. This way we bind the function and message ID together.


The next step is to give the implementation of the OnMyMessage() function in the .cpp file of the main dialog.


In this way, the sub-dialog can send a message to the main dialog through the SendMessage() function, and the main dialog will receive the message in the afx_msg LRESULT OnMyMessage(WPARAM wParam, LPARAM lParam); function.


///////////////////////////////////////////--------------------------------------------------------------////////////////////////

2" Send a message between two sub-dialogs

The processing of sending messages between the two sub-dialogs is roughly the same as the above, the difference is in the SendMessage() function. So just write the code for this:

"Sub-dialog 1" sends a message to "Sub-dialog 2": write the following SendMessage() function where the message needs to be sent.


The "C message sending Dlg" in the figure is the class of the main dialog; m_pDlg2 is an object of "sub-dialog 2" defined in the main dialog class.

Note: When two sub-dialogs perform message transmission, these two dialogs must exist at the same time, otherwise an error will be reported. (Both dialogs can be made non-modal).


Reference: http://blog.sina.cn/dpool/blog/s/blog_8f479b390102xa4n.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325691962&siteId=291194637