Introduction to MFC Programming 11 (Dialog box: modal dialog box and its pop-up process)

Source: Internet
Author: User

Add Calculator dialog box after you have done this, I believe that there are some solutions to the dialog-based program, and a good start will be beneficial to future study. As you strike, this section tells you what dialog boxes and non-modal dialogs are and how modal dialogs pop up.

Modal dialog box and non modal dialog box

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 Wig Calculator dialog is actually a modal dialog box.

Second, modal dialog box is how to eject

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. The following is a brief talk about where it came out, and then re-build a new dialog box and pop her up so that we can use the modal dialog box more flexibly after practice.

When you open the Addition.cpp file, you can see that the Cadditionapp class has a InitInstance () function, which 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, giving the code and annotating it:

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.

Third, add a new dialog box and pop it

Add a dialog box to the addition Calculator program to ask the user if they are sure they want to calculate before calculating.

1. Right-click on "Dialog" in Resource View to select "Insert Dialog", create a new dialog template, change its ID to "prompt", and refer to "Add Control for dialog box" , add a static text box to the dialog template, caption change to "Are you sure you want to do an addition calculation?" , adjust the dialog box size and position of the control, and the final dialog box template such as:

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

3, we want to click the "Calculate" button to pop up this dialog box, then the "Calculate" button in the message handler function Onbnclickedaddbutton () To access the prompt dialog box class, so in order to access the Ctipdlg class, Include the Ctipdlg header file in the AdditionDlg.cpp: #include "TipDlg.h".

4, modify the function body of Onbnclickedaddbutton (), before all the code, constructs the Ctipdlg class object Tipdlg, and through 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:

C + + code:

5, Test. After compiling the program, enter Summand and Addend on the dialog, click "Calculate", pop up the prompt dialog to ask if the calculation is done, if you choose "OK", the prompt dialog box exits, and the dialog box displays Summand and Addend, and if you choose "Cancel", the prompt dialog box will also exit. However, the main dialog box is displayed and unchanged, that is, there is no addition calculation.

Introduction to MFC Programming 11 (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.