Summary
This article is still about the message mechanism. In the previous chapter, we talked about how to use threads to communicate with windows. Today we will talk about the communication principles between windows.
Prerequisites
You need to know what the message mechanism is, or you have read the previous chapter.
Procedure
Create a project named "multiwindows" and use
Unicode encoding. The specific interface can be as follows: First, add a new window and layout it as follows: after adding a window, add a class file for the window as follows: after adding the message, you can add the message function. This time, the difference is that there is only one window in the previous articles, and the messages are all in one window. Now it is changed into two windows. In order to make the two windows know the Message ID of the other party, it is decided in. the Message ID (previously in * DLG. h). As mentioned in the previous article, you just need to change the position during the declaration. The message is the main window message. The subwindow message is: after the message is added, you can declare the Message ID in project name. h.
[CPP]
View plaincopyprint?
- # Define wm_main_msg wm_user + 0x01001
- # Define wm_sub_msg wm_user + 0x02001
#define WM_MAIN_MSG WM_USER+0x01001#define WM_SUB_MSG WM_USER+0x02001
And then in the project name DLG. h. Declare the window handle variable of the subwindow to add and click the code for "Opening subwindow (& O)" and "sending messages to subwindow (& S)" on the primary window:
[CPP]
View plaincopyprint?
- // Open the subwindow (& O)
- Void cmultiwindowsdlg: onbnclickedbutton1 ()
- {
- If (DLG = NULL)
- {
- DLG = new cmysubdialog ();
- DLG-> Create (idd_dialog1, this );
- }
- If (DLG = NULL) Return (void) MessageBox (_ T ("the sub-window handle is empty! "));
- DLG-> showwindow (sw_shownormal );
- }
- // Send a message to the subwindow (& S)
- Void cmultiwindowsdlg: onbnclickedbutton2 ()
- {
- Cstring stredit;
- Getdlgitemtext (idc_edit1, stredit );
- If (DLG = NULL) Return (void) MessageBox (_ T ("the sub-window handle is empty! "));
- DLG-> sendmessage (wm_sub_msg, (wparam) & stredit );
- }
// Open the subwindow (& O) void cmultiwindowsdlg: onbnclickedbutton1 () {If (DLG = NULL) {DLG = new cmysubdialog (); DLG-> Create (idd_dialog1, this);} If (DLG = NULL) Return (void) MessageBox (_ T ("the handle of the subwindow is empty! "); DLG-> showwindow (sw_shownormal);} // send a message to the subwindow (& S) void cmultiwindowsdlg: onbnclickedbutton2 () {cstring stredit; getdlgitemtext (idc_edit1, stredit); If (DLG = NULL) Return (void) MessageBox (_ T ("the handle of the subwindow is empty! "); DLG-> sendmessage (wm_sub_msg, (wparam) & stredit );}
Then add code to the message in the main window:
[CPP]
View plaincopyprint?
- Afx_msg lresult cmultiwindowsdlg: onmainmsg (wparam, lparam)
- {
- Cstring * strmsg = (cstring *) wparam;
- Setdlgitemtext (idc_edit1, * strmsg );
- Return 0;
- }
afx_msg LRESULT CMultiWindowsDlg::OnMainMsg(WPARAM wParam, LPARAM lParam){CString* strMsg = (CString*)wParam;SetDlgItemText(IDC_EDIT1,*strMsg);return 0;}
Send a message (& S) to the parent window and add code to the message in the subdialog box in the same way.[CPP]
View plaincopyprint?
- // Message code
- Afx_msg lresult cmysubdialog: onsubmsg (wparam, lparam)
- {
- // Forcibly convert wparam to the cstring type
- Cstring * strmsg = (cstring *) wparam;
- // Edit the text in the dialog box
- Setdlgitemtext (idc_edit1, * strmsg );
- Return 0;
- }
- // Send a message to the parent window (& S)
- Void cmysubdialog: onbnclickedbutton1 ()
- {
- // Obtain the text in the edit box
- Cstring stredit;
- Getdlgitemtext (idc_edit1, stredit );
- // Obtain the handle of the parent window
- Hwnd = This-> getparent ()-> getsafehwnd ();
- // Send a message to the parent window
- If (hwnd = NULL) Return (void) MessageBox (_ T ("failed to get the parent window handle! "));
- : Sendpolicymessage (hwnd, wm_main_msg, (wparam) & stredit, null );
- }
// Message code afx_msg lresult cmysubdialog: onsubmsg (wparam, lparam) {// forcibly convert wparam to cstring type cstring * strmsg = (cstring *) wparam; // setdlgitemtext (idc_edit1, * strmsg); Return 0;} // send a message (& S) to the parent window void cmysubdialog: onbnclickedbutton1 () {// obtain the text cstring stredit in the edit box; getdlgitemtext (idc_edit1, stredit); // obtain the parent window handle hwnd = This-> getparent ()-> getsafehwnd (); // send the message if (hwnd = NULL) r to the parent window Eturn (void) MessageBox (_ T ("failed to get the parent window handle! ");: Sendpolicymessage (hwnd, wm_main_msg, (wparam) & stredit, null );}
Compile and run
Summary and Expansion
In fact, messages are very useful, not just here!
Download example
: Click to download
Summary
This article is still about the message mechanism. In the previous chapter, we talked about how to use threads to communicate with windows. Today we will talk about the communication principles between windows.
Prerequisites
You need to know what the message mechanism is, or you have read the previous chapter.
Procedure
Create a project named "multiwindows" and use
Unicode encoding. The specific interface can be as follows: First, add a new window and layout it as follows: after adding a window, add a class file for the window as follows: after adding the message, you can add the message function. This time, the difference is that there is only one window in the previous articles, and the messages are all in one window. Now it is changed into two windows. In order to make the two windows know the Message ID of the other party, it is decided in. the Message ID (previously in * DLG. h). As mentioned in the previous article, you just need to change the position during the declaration. The message is the main window message. The subwindow message is: after the message is added, you can declare the Message ID in project name. h.
[CPP]
View plaincopyprint?
- # Define wm_main_msg wm_user + 0x01001
- # Define wm_sub_msg wm_user + 0x02001
#define WM_MAIN_MSG WM_USER+0x01001#define WM_SUB_MSG WM_USER+0x02001
And then in the project name DLG. h. Declare the window handle variable of the subwindow to add and click the code for "Opening subwindow (& O)" and "sending messages to subwindow (& S)" on the primary window:
[CPP]
View plaincopyprint?
- // Open the subwindow (& O)
- Void cmultiwindowsdlg: onbnclickedbutton1 ()
- {
- If (DLG = NULL)
- {
- DLG = new cmysubdialog ();
- DLG-> Create (idd_dialog1, this );
- }
- If (DLG = NULL) Return (void) MessageBox (_ T ("the sub-window handle is empty! "));
- DLG-> showwindow (sw_shownormal );
- }
- // Send a message to the subwindow (& S)
- Void cmultiwindowsdlg: onbnclickedbutton2 ()
- {
- Cstring stredit;
- Getdlgitemtext (idc_edit1, stredit );
- If (DLG = NULL) Return (void) MessageBox (_ T ("the sub-window handle is empty! "));
- DLG-> sendmessage (wm_sub_msg, (wparam) & stredit );
- }
// Open the subwindow (& O) void cmultiwindowsdlg: onbnclickedbutton1 () {If (DLG = NULL) {DLG = new cmysubdialog (); DLG-> Create (idd_dialog1, this);} If (DLG = NULL) Return (void) MessageBox (_ T ("the handle of the subwindow is empty! "); DLG-> showwindow (sw_shownormal);} // send a message to the subwindow (& S) void cmultiwindowsdlg: onbnclickedbutton2 () {cstring stredit; getdlgitemtext (idc_edit1, stredit); If (DLG = NULL) Return (void) MessageBox (_ T ("the handle of the subwindow is empty! "); DLG-> sendmessage (wm_sub_msg, (wparam) & stredit );}
Then add code to the message in the main window:
[CPP]
View plaincopyprint?
- Afx_msg lresult cmultiwindowsdlg: onmainmsg (wparam, lparam)
- {
- Cstring * strmsg = (cstring *) wparam;
- Setdlgitemtext (idc_edit1, * strmsg );
- Return 0;
- }
afx_msg LRESULT CMultiWindowsDlg::OnMainMsg(WPARAM wParam, LPARAM lParam){CString* strMsg = (CString*)wParam;SetDlgItemText(IDC_EDIT1,*strMsg);return 0;}
Send a message (& S) to the parent window and add code to the message in the subdialog box in the same way.[CPP]
View plaincopyprint?
- // Message code
- Afx_msg lresult cmysubdialog: onsubmsg (wparam, lparam)
- {
- // Forcibly convert wparam to the cstring type
- Cstring * strmsg = (cstring *) wparam;
- // Edit the text in the dialog box
- Setdlgitemtext (idc_edit1, * strmsg );
- Return 0;
- }
- // Send a message to the parent window (& S)
- Void cmysubdialog: onbnclickedbutton1 ()
- {
- // Obtain the text in the edit box
- Cstring stredit;
- Getdlgitemtext (idc_edit1, stredit );
- // Obtain the handle of the parent window
- Hwnd = This-> getparent ()-> getsafehwnd ();
- // Send a message to the parent window
- If (hwnd = NULL) Return (void) MessageBox (_ T ("failed to get the parent window handle! "));
- : Sendpolicymessage (hwnd, wm_main_msg, (wparam) & stredit, null );
- }
// Message code afx_msg lresult cmysubdialog: onsubmsg (wparam, lparam) {// forcibly convert wparam to cstring type cstring * strmsg = (cstring *) wparam; // setdlgitemtext (idc_edit1, * strmsg); Return 0;} // send a message (& S) to the parent window void cmysubdialog: onbnclickedbutton1 () {// obtain the text cstring stredit in the edit box; getdlgitemtext (idc_edit1, stredit); // obtain the parent window handle hwnd = This-> getparent ()-> getsafehwnd (); // send the message if (hwnd = NULL) r to the parent window Eturn (void) MessageBox (_ T ("failed to get the parent window handle! ");: Sendpolicymessage (hwnd, wm_main_msg, (wparam) & stredit, null );}
Compile and run
Summary and Expansion
In fact, messages are very useful, not just here!
Download example
: Click to download