VS2010/MFC Programming (dialog box: modal dialog box and its pop-up process)

Source: Internet
Author: User

Talk about modal dialogs and non modal dialogs, and how modal dialogs pop up.

A. modal dialogs and non-modal dialog boxes

The Windows dialog is divided into two categories: modal dialogs and non-modal dialog boxes.

Modal dialog box is a dialog box, when it pops up, the other windows of the application will no longer accept user input, only the dialog box in response to user input, after the corresponding action to exit, other windows can continue to interact with the user.

The non-modal dialog box is that when it pops up, the other windows of the program can still respond to user input. The non-modal dialog box is typically used to display informational messages and so on.

You know the Windows system very well, I believe both of these dialog boxes should have encountered. The previous Add Calculator dialog is a modal dialog box.

two. How the modal dialog box pops up

After all, the addition Calculator program is mostly MFC automatically generated, the dialog box how to pop out of the people may not be very clear. Chicken Peck Rice below simply say where it is to play out, and then re-build a new dialog box and pop it, so that we will be more flexible to use the modal dialog box.

You open the Addition.cpp file, you can see the Cadditionapp class has a InitInstance () function, in the MFC application framework analysis mentioned this function, but that is the single document application class, the function body is not the same, However, it is the initialization of an instance of the App class.

The second part of the InitInstance () function has a piece of code that defines the dialog object and pops up the dialog box, and the Chicken Peck meter gives the code and notes it:

Cadditiondlg Dlg;//defining a dialog Class Cadditiondlg object Dlgm_pMainWnd = &dlg;//set Dlg as the main windowINT_PTR Nresponse = dlg. DoModal ();//The dialog box appears dlg and assigns the return value of the DoModal function (the ID of the click button on exit) to Nresponseif(Nresponse = = IDOK)//determines whether the return value is an OK button (its ID is idok, chicken peck rice has deleted it){       //Todo:place code here-handle when the dialog is//dismissed with OK}   Else if(Nresponse = = IDCANCEL)//determines whether the return value is the Cancel button (whose ID is idcancel, and the chicken pecked its caption to "Exit"){       //Todo:place code here-handle when the dialog is//dismissed with Cancel}

Pop-up dialog a key function is the DoModal () function of the dialog class. CDialog: The prototype of the:D Omodal () function is:

Virtual INT_PTR DoModal ();

Return value: An integer value that specifies the value of the Nresult parameter passed to Cdialog::enddialog, which is used to close the dialog box. Returns 1 if the function cannot create a dialog box, or returns Idabort if there are other errors.

The call to the dialog pops up and the return value is the ID of the button that was clicked when the dialog was exited, for example, when we clicked the "Exit" button, then the DoModal return value was idcancel.

three. Add a new dialog box and pop it

You can take a complete look at the dialog box to add and pop-up process.

1. Based on the method described in "Creating a dialog template and Modifying dialog Box Properties", in Resource View, right-click on "Dialog" and select "Insert Dialog", create a new dialog template, modify its ID to Idd_tip_dialog, Caption change to prompt, and then refer to add Control for dialog box, add a static text box on the dialog template (static text), caption to "Are you sure you want to do the addition calculation?" , then modify the OK button's caption to OK, the Cancel button's caption to cancel, and finally adjust the position of each control and the size of the dialog box. The final dialog box template is as follows:

2. Based on the method of creating the dialog class in "Create dialog class and add control variable", right-click on the dialog template to select "Add Class ...", Pop up the Add Class dialog box, set "class name" to Ctipdlg, click "OK". In Solution Explorer, you can see the header file TipDlg.h and source file TipDlg.cpp that generated the Ctipdlg class.

3. In order to access the Ctipdlg class, we need to access the prompt dialog class in the Message handler function Onbnclickedaddbutton () of the Calculate button after the point "Calculate" button. Include the Ctipdlg header file in the AdditionDlg.cpp: #include "TipDlg.h".

4. Modify the function body of the Onbnclickedaddbutton (), before all the code, construct the object Tipdlg of the Ctipdlg class, and pass the statement tipdlg.domodal (); pop-up dialog, finally Judge DoModal () The return value of the function is IDOK or idcancel to determine whether to proceed with the calculation. The Onbnclickedaddbutton () function is modified as follows:

voidCadditiondlg::onbnclickedaddbutton () {//Todo:add your control notification handler code hereINT_PTR Nres;//The return value used to hold the DoModal functionCtipdlg Tipdlg; //constructs an instance of the dialog class CtipdlgNres = Tipdlg.domodal ();//Popup dialog box    if(IDCANCEL = = nres)//determines whether the return value is idcancel when the dialog box exits, or returns if it is, or continues execution down        return; //Save the data in each control to the appropriate variableUpdateData (TRUE); //will be added and assigned to M_editsum by Addend and Addend.M_editsum = M_editsummand +M_editaddend; //updates the corresponding control based on the values of each variable. And the edit box will display the value of the M_editsumUpdateData (FALSE); }  

5. Test. After compiling the program, enter Summand and Addend on the dialog, click "Calculate", the popup prompt dialog asks whether the calculation is done, if "OK" is selected, the prompt dialog box exits, and the Summand and Addend are displayed on the main dialog box, and if "Cancel" is selected, the prompt dialog box exits. However, the main dialog box is displayed and unchanged, that is, there is no addition calculation.

VS2010/MFC Programming (dialog box: modal dialog box and its pop-up process)

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.