VC window creation and the transfer of data between windows, passing messages (modal, non-modal)

Source: Internet
Author: User

In VC + +, open dialog box is generally using the DoModal () function call modal dialog box, but modal dialog box can only be in the pop-up window to operate, and can not operate on the parent window, and can not pass data to the parent window, according to the author's research found that The mode of modeless dialog box can solve this problem well.

In VS2008, create a new project project with an MFC application, select "Based on dialog" In the MFC Application Wizard that pops up, cancel "Use Unicode Library", and click Finish. In Resource View, add a dialog box with the default ID of IDD_DIALOG1.

Double-click the Idd_dialog1 dialog box, and in the MFC Class Wizard that pops up, the class name is filled in Csondialog, the base class selects CDialog, and clicks Finish. This allows us to associate the newly created IDD_DIALOG1 with a CDialog-based class.

Add a button to the parent window and double-click to enter the message response function for this button. On top contains the Csondialog header file #include "SonDialog.h". If you write the following code in the message response function:

Csondialogsonwnd;

Sonwnd.domodal ();

When you run the button on the parent window, you can see that idd_dialog1 is popping up, but you can only operate on IDD_DIALOG1 and cannot manipulate the parent window. If you want to be able to manipulate the parent window after the child window is ejected, you will need to use the modeless Dialog Mode pop-up child window.

MFC has a Create (UINT nidtemplate, CWnd *pparentwnd =0) in the CDialog class, which creates a dialog where the parameter nidtemplate is the ID of the dialog that you want to create. There is also a function ShowWindow (int ncmdshow) to display the created dialog. Write the following code in the message response function:

Csondialogsonwnd;

Sonwnd.create (IDD_DIALOG1);

Sonwnd.showwindow (Sw_show);

After running, press the button on the parent window to see the window flash, and then it disappears. This is because the object Sonwnd is a local object that exits the message response function after running the Sonwnd.showwindow (sw_show) statement, so the Sonwnd object is destroyed. If the window still exists after you want to exit the message response function, you need to define SONWND as a global variable. So add a csondialog sonwnd definition in ProjectDlg.h, and because VC + + compiles the header file at compile time, you also need to include Csondialog header file in ProjectDlg.h #include " SonDialog.h ", so in the ProjectDlg.cpp, you can delete the SonDialog.h. Then add the following code in the button's message response function:

Sonwnd.create (IDD_DIALOG1);

Sonwnd.showwindow (Sw_show);

We found that Idd_dialog1 was created and kept. However, there is no way to communicate data with the parent window. According to the lookup data, we find that there is a special pointer in C + + that points to the current window, which is the this pointer. We call each other's data by passing the this pointer.

In the Csondialog class, we add a global pointer variable Cprojectdlg *m_pfaher to the parent window and add a function wndcreate (Cprojectdlg *pparent) with the following code:

Voidcsondialog::wndcreate (Cprojectdlg *pparent)

{

Create (IDD_DIALOG1); Create a dialog box

ShowWindow (Sw_show); Show dialog box

M_pfather = pparent; Passing the parent window pointer in

}

This function calls the Create () and ShowWindow () functions in the CDialog class to create and display dialog boxes, passing the pointer of the parent window to the child window using the parameter pass method. In the PROJECTDLG.CPP message response function of the parent window, we add the following code:

Sonwnd.wndcreate (this);

There was an error after the compile run. Because the SonDialog.h is included in the header file of the ProjectDlg.h, and the ProjectDlg.h is included in the SonDialog.h, there are two ways to resolve the problem when the program compiles with errors that the header file repeats.

The first option is to add the precompiled command to the two header files #ifndef #define #endif命令, join the top of SonDialog.h

#ifndefSONDIALOG

#defineSONDIALOG

Join at the bottom

#endif

Join at the top of ProjectDlg.h

#ifndefRPOJECTDLG

#definePROJECTDLG

Join at the bottom

#endif

The sense of the above statement block is that if the SONDIALOG/PROJECTDLG is not defined, then define SONDIALOG/PROJECTDLG, and if Sondialog/projectdlg is defined, jump directly to #endif, This is a good way to avoid being repeatedly defined. This method I used to use when programming, but I do not know why a few recent writing programs This method is ineffective, so I came up with another way.

The second approach is to take a pointer variable that avoids the definition of a specific type in the header file, bypassing the problem of repeated inclusion of the header file with a method that defines the null pointer. Because in the parent window, the object pointing to the child window must be a global variable in order to ensure that the child window is displayed until it is destroyed. So in the parent window ProjectDlg.h have to include SonDialog.h header file, so you can only find a way in SonDialog.h. In fact, we found that the problem can be solved by simply defining a null pointer in SonDialog.h. The specific methods are as follows:

The SonDialog.h does not contain the ProjectDlg.h header file, nor does it define the Cprojectdlg object, but instead defines a null pointer lpvoidm_pfather that changes the parameters of the Wndcreate () function to LPVoid ppaernt , and then add the following code in the Wndcreate () function:

Voidcsondialog::wndcreate (LPVOID pparent)

{

Create (IDD_DIALOG1); Create a dialog box

ShowWindow (Sw_show); Show dialog box

M_pfather = pparent; Passing the parent window pointer in

}

In this way, the this pointer of the parent window is passed in to M_pfather or a pointer to any object. Whenever you need to call a function in a SonDialog.cpp function or change some of the parent window's variables, include the header file ProjectDlg.h in the CPP file, and add the code at the start of the function:

Cprojectdlg*main;

Main = (cprojectdlg*) m_pfather; Force the LPVOID type to be converted

Main->

You can use the pointer main to manipulate the parent window. This allows the information in the two dialog boxes to be passed on to each other.

In addition, when creating modeless dialogs, be aware that rewriting the OnOK () and OnCancel () two functions, adding the Destorywindow () function, the OnOK () and the OnCancel () function, does not destroy the window, but rather makes the window invisible. If you do not destroy the window, an error occurs the next time the child window is opened again.

The above code runs through Windows 7 Home normal edition +visual Studio 2008sp1.

==============

Modal dialog box pass parameter http://blog.csdn.net/xiaobai1593/article/details/6591893

Parent Pass to Child

[CPP] view plain copy void Cdddlg::onbnclickedbutton1 () {//TODO: Add control Notification handler code CDLG2 DLG here; Dlg.strname= "Xipeng is a dog!!!"; The public member variable dlg defined in/cdlg2.   DoModal (); }
Overrides the CDlg2 OnInitDialog function.

Constructor initialization in CDlg2 StrName

[CPP] view plain copy BOOL Cdlg2::oninitdialog () {cdialog::oninitdialog (); TODO: Add an extra initialization m_edit2 here.       SetWindowText (StrName);       UpdateData (FALSE);        return TRUE; Return TRUE unless your set the focus to a control//exception: OCX property page should be returned FALSE}
Child to the parent

Creates a pointer to a parent window in a child window and assigns a value to its member variable (which can also be implemented in modal dialog boxes)

[CPP] view plain copy void Bdlg::onok () {//Todo:add extra validation here Adlg * adlg= (Adlg       *) this->getparent ();       adlg->m_edit= "I Hate U";          Adlg->updatedata (FALSE); Cdialog::onok ();

Best solution

Best Practices:

is looking at the Sun Xin VC video time to see, is really an expert ah.

Because the execution of the modeless dialog box does not block the execution of the main dialog, most of the time you can only use modal dialogs.

dialog box after the DoModal () function is executed, the window is destroyed, but the object still exists, so you can still access its member variables.

That is, you can access the member variables of the modal dialog object directly in the main dialog box without having to get a pointer to the parent dialog box in the Child dialog box to pass the argument.

[CPP]   View plain copy void adlg::onpopbtn ()       {       if (Updatedlg.domodal () ==idok)     

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.