modal dialog boxes and non-modal dialogs in MFC

Source: Internet
Author: User

Modal dialog box creation:

Mydialog Mydlg;mydlg. DoModal ()

Only this modal dialog box is currently running and the main window is stopped until the modal dialog box exits, allowing the primary window to run.

Modal dialog box Close order:

OnClose: Press close symbol X to respond to Wm_close message

OnKillFocus: Window is about to lose input focus, respond to Wm_killfocus message

OnDestroy: Response Wm_destroy Message when window is about to be destroyed

OnNcDestroy: Window is destroyed or, in response to Wm_ncdestroy message

PostNcDestroy: Called by Onncdesyroy, is a CWnd virtual function

The non-modal dialog box is usually created from new:

New Mydialog;mydlg->create (idd_dialog1,this); Mydlg->showwindow (sw_show);

Non-modal dialog box close order:
OnClose: Press close symbol X to respond to Wm_close message

OnDestroy: Response Wm_destroy Message when window is about to be destroyed

OnNcDestroy: Window is destroyed or, in response to Wm_ncdestroy message

PostNcDestroy: Called by Onncdesyroy, CWnd function

In order to destroy the dialog pointer, you can overload PostNcDestroy in the dialog class and add delete this.

void Mydialog::P Ostncdestroy () {    CDialogEx::P Ostncdestroy ();     Delete  This ;}

Attention:

After the OnClose function is finished, the call in Oncannel,oncannel is actually called EndDialog (), which is used to close the modal dialog box. It works as follows: EndDialog is used to end the DoModal loop, DoModal has a DestroyWindow call after the internal end. But for the non-modal dialog box, because it does not have a domodal loop, it will not automatically call the DestroyWindow function, there is no subsequent operations such as OnDestroy, so the non-modal dialog box to overload the Oncannel function, directly execute destroywindow.

void Mydialog::oncancel () {    DestroyWindow ();}

Considerations for turning off the non-modal dialog box [1] are also described on MSDN:

When you implementa modeless dialog box, always override the OnCancel member function and call DestroyWindow from within I T. Don ' t call Thebase class Cdialog::oncancel, because it calls EndDialog, which would make the dialog box invisible but WI Llnot destroy it. should also override PostNcDestroy for modeless dialog boxes inorder to delete this, since modeless dialog boxes Areus Ually allocated with new. Modal dialog boxes is usuallyconstructed on the frame and does not need PostNcDestroy cleanup
In addition to clicking the Close Symbol Close dialog box, MFC can also close the dialog box by OnOK (), Oncancle (), which correspond to the "OK" and "Cancel" buttons on the default dialog interface respectively, and the two functions are virtual member functions of the CDialog class, and two functions have one thing in common. , EndDialog will be called.
Therefore, to close the non-modal dialog box with OnOK or oncancle, you still need to overload it to invoke DestroyWindow.
Now implement an application:
In a dialog-based MFC program, click the button, pop up another sub-dialog box, enter a string in the popup sub-dialog box, click the button, the Child dialog box closes, the main dialog box displays the input string.
Right-click the dialog tab in the Resource View, select Insert dialog, double-click the inserted dialog box, enter the MFC Add Class Wizard, and Add a dialog box class that is bound to the dialog box:

We will add the dialog edited into the form below. The edit box receives input, clicks Button1, takes input from the edit box, destroys the dialog, and passes the result to the parent dialog box.

The main dialog box is also edited in the same form, the edit box is used to display the output, and clicking Button1 will result in a sub-dialog box.

The following sections implement pop-up modal and non-modal dialogs respectively.

(1) Modal dialog box
Parent dialog Box Button response:

void Cmodaltestdlg::onbnclickedbutton1 () {    //  TODO: Add control notification handler code here     Mydialog Mydlg;     if (idok==Mydlg. DoModal ())    {        outstr=mydlg.instr;    }    GetDlgItem (idc_editoutput),SetWindowText (outstr);

The button response for the Child dialog box:

void Mydialog::onbnclickedbutton1 () {    //  TODO: Add control notification handler code here    GetDlgItem (idc_ Editinput)->getwindowtext (instr); // take input string    Cdialogex::onok (); // Close the dialog box and return to Idok}

An MFC Window object consists of two aspects: the Window object encapsulated by the window, the hwnd stored in the M_hwnd Member (window handle), and the Window object itself being a C + + object. To delete an MFC window object, you should first delete the Window object that encapsulates the window, and then delete the Window object itself. When DoModal returns, the modal dialog window has been destroyed, but the dialog class still exists and the unit can take the input variable directly from the Mydlg. Mydlg belongs to the local variable, OnBnClickedButton1 after execution, automatically destroyed.

(2) Non modal dialog box

Parent dialog Box Button response:

void Cmodelesstestdlg::onbnclickedbutton1 () {    //  TODO: Add control Notification Handler code in this    new Mydialog;    Mydlgthis);    Mydlg-ShowWindow (sw_show);}

Child Dialog Button Response:

void Mydialog::onbnclickedbutton1 () {    //  TODO: Add control notification handler code here    CString *instr=New  CString;    GetDlgItem (idc_editinput)->getwindowtext (*instr);    HWND Parhwnd=getparent ()->m_hwnd; // Get parent window handle    ::P ostmessage (Parhwnd,wm_update, (WPARAM) instr,0);   sends a message to the parent window     OnOK ();}

In order to pass the string from the Child dialog box to the parent dialog box, we used PostMessage to send the custom message wm_update. Because the parent dialog box is returned immediately after it has been create a non-modal dialog box, it does not have to wait for the result to end directly as the modal dialog box does.
The Oncancle Close dialog box is used in the Child dialog box, so it needs to be overloaded:

void Mydialog::oncancel () {    //  TODO: Add private code here and/or call base class     //Cdialogex::o Ncancel ();     DestroyWindow ();}

Additional overloads are required for PostNcDestroy:

void Mydialog::P Ostncdestroy () {    //  TODO: Add private code here and/or call base class     CDialogEx:: PostNcDestroy ();     Delete  This ;}

Virtual functions to be overloaded can be found through the MFC Class Wizard:

Define a message response function in the parent dialog class to convert the message parameters from the Child dialog box to the string display:

// Message response function LRESULT cmodelesstestdlg::onupdate (WPARAM WPARAM, LPARAM LPARAM) {    * outstr = (cstring*) (WPARAM);    GetDlgItem (idc_editoutput)->setwindowtext (*outstr);     Delete outstr;     return true ;}

Source code: MFC modal modal non modal dialog box

Reference:

[1]https://msdn.microsoft.com/en-us/library/132s802t.aspx

[2]http://blog.csdn.net/hanyujianke/article/details/8507064

[3]http://blog.csdn.net/xiaominggunchuqu/article/details/49895325

modal dialog boxes and non-modal dialogs in MFC

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.