MFC learning notes-communication between multiple windows.

Source: Internet
Author: User
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?
  1. # Define wm_main_msg wm_user + 0x01001
  2. # 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?
  1. // Open the subwindow (& O)
  2. Void cmultiwindowsdlg: onbnclickedbutton1 ()
  3. {
  4. If (DLG = NULL)
  5. {
  6. DLG = new cmysubdialog ();
  7. DLG-> Create (idd_dialog1, this );
  8. }
  9. If (DLG = NULL) Return (void) MessageBox (_ T ("the sub-window handle is empty! "));
  10. DLG-> showwindow (sw_shownormal );
  11. }
  12. // Send a message to the subwindow (& S)
  13. Void cmultiwindowsdlg: onbnclickedbutton2 ()
  14. {
  15. Cstring stredit;
  16. Getdlgitemtext (idc_edit1, stredit );
  17. If (DLG = NULL) Return (void) MessageBox (_ T ("the sub-window handle is empty! "));
  18. DLG-> sendmessage (wm_sub_msg, (wparam) & stredit );
  19. }
// 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?
  1. Afx_msg lresult cmultiwindowsdlg: onmainmsg (wparam, lparam)
  2. {
  3. Cstring * strmsg = (cstring *) wparam;
  4. Setdlgitemtext (idc_edit1, * strmsg );
  5. Return 0;
  6. }
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?

  1. // Message code
  2. Afx_msg lresult cmysubdialog: onsubmsg (wparam, lparam)
  3. {
  4. // Forcibly convert wparam to the cstring type
  5. Cstring * strmsg = (cstring *) wparam;
  6. // Edit the text in the dialog box
  7. Setdlgitemtext (idc_edit1, * strmsg );
  8. Return 0;
  9. }
  10. // Send a message to the parent window (& S)
  11. Void cmysubdialog: onbnclickedbutton1 ()
  12. {
  13. // Obtain the text in the edit box
  14. Cstring stredit;
  15. Getdlgitemtext (idc_edit1, stredit );
  16. // Obtain the handle of the parent window
  17. Hwnd = This-> getparent ()-> getsafehwnd ();
  18. // Send a message to the parent window
  19. If (hwnd = NULL) Return (void) MessageBox (_ T ("failed to get the parent window handle! "));
  20. : Sendpolicymessage (hwnd, wm_main_msg, (wparam) & stredit, null );
  21. }
// 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?
  1. # Define wm_main_msg wm_user + 0x01001
  2. # 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?
  1. // Open the subwindow (& O)
  2. Void cmultiwindowsdlg: onbnclickedbutton1 ()
  3. {
  4. If (DLG = NULL)
  5. {
  6. DLG = new cmysubdialog ();
  7. DLG-> Create (idd_dialog1, this );
  8. }
  9. If (DLG = NULL) Return (void) MessageBox (_ T ("the sub-window handle is empty! "));
  10. DLG-> showwindow (sw_shownormal );
  11. }
  12. // Send a message to the subwindow (& S)
  13. Void cmultiwindowsdlg: onbnclickedbutton2 ()
  14. {
  15. Cstring stredit;
  16. Getdlgitemtext (idc_edit1, stredit );
  17. If (DLG = NULL) Return (void) MessageBox (_ T ("the sub-window handle is empty! "));
  18. DLG-> sendmessage (wm_sub_msg, (wparam) & stredit );
  19. }
// 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?
  1. Afx_msg lresult cmultiwindowsdlg: onmainmsg (wparam, lparam)
  2. {
  3. Cstring * strmsg = (cstring *) wparam;
  4. Setdlgitemtext (idc_edit1, * strmsg );
  5. Return 0;
  6. }
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?

  1. // Message code
  2. Afx_msg lresult cmysubdialog: onsubmsg (wparam, lparam)
  3. {
  4. // Forcibly convert wparam to the cstring type
  5. Cstring * strmsg = (cstring *) wparam;
  6. // Edit the text in the dialog box
  7. Setdlgitemtext (idc_edit1, * strmsg );
  8. Return 0;
  9. }
  10. // Send a message to the parent window (& S)
  11. Void cmysubdialog: onbnclickedbutton1 ()
  12. {
  13. // Obtain the text in the edit box
  14. Cstring stredit;
  15. Getdlgitemtext (idc_edit1, stredit );
  16. // Obtain the handle of the parent window
  17. Hwnd = This-> getparent ()-> getsafehwnd ();
  18. // Send a message to the parent window
  19. If (hwnd = NULL) Return (void) MessageBox (_ T ("failed to get the parent window handle! "));
  20. : Sendpolicymessage (hwnd, wm_main_msg, (wparam) & stredit, null );
  21. }
// 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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.