) MFC modal dialog box and non-Modal Dialog Box

Source: Internet
Author: User

Http://blog.csdn.net/candyliuxj/article/details/6736032

The dialog box in MFC can be in either model dialog box or modeless dialog box ).

1. Modal Dialog Box)

InProgramIf a modal dialog box appears during running, the message cannot be sent in the main window until the Modal Dialog Box exits.

Click OK in the modal dialog box. The modal dialog box is destroyed.

Create a modal dialog boxCode:

// Create a modal dialog box ctestdialog TD; TD. domodal ();

 

Here, ctestdialog is the dialog box class that I created and associated with a dialog box resource.

You can create a class variable for the layout modal dialog box without worrying that it will be destroyed as the function returns. One function of the domodal () function is that this modal dialog box can only be run currently, and the main window is stopped until the Modal Dialog Box exits.

The domodal () function also displays the dialog box, so you do not need to call other functions to display the dialog box.

2. modaless dialog box)

When the program is running, if a non-modal dialog box is displayed, the main window can also send messages.

Click OK in the non-modal dialog box. The non-modal dialog box is not destroyed, but hidden. If you want to click the OK button and the non-modal dialog box is destroyed, the ctestdialog class must overload the virtual function onok () of its base class cdialog, and call destroywindow () in this function () to destroy this dialog box.

If you create a non-modal dialog box in the same way as above:

 
Ctestdialog TD; TD. Create (idd_dialog1); // create a non-Modal Dialog Box TD. showwindow (sw_shownormal); // display the non-Modal Dialog Box

 

Then, you will find that this dialog box cannot be displayed during running. This is because the declared dialog box variable TD is a local variable, but TD is also destructed when this function is returned, so this dialog box cannot be displayed.

To create a non-modal dialog box, you must declare a pointer variable pointing to the ctestdialog class and call showwindow () to display the dialog box. There are two ways to create:

(1) create a non-modal dialog box using local variables

 

 
// Use local variables to create a non-Modal Dialog Box ctestdialog * PTD = new ctestdialog (); PTD-> Create (idd_dialog1 ); // create a non-Modal Dialog Box PTD-> showwindow (sw_shownormal); // display the non-Modal Dialog Box

 

The pointer is placed in the stack when it is declared, and will be destroyed only after the entire application is closed, so the dialog box can be displayed normally.

Although this method does not affect the running of the program, the memory pointed to by the pointer PTD is unavailable, and such programming is very bad.

(2) create a non-Modal Dialog Box Using member variables

First, declare a pointer variable in the header file of the class you want to write:

 
PRIVATE: ctestdialog * PTD;

 

Then, in the corresponding CPP file, add the following code at the location of the dialog box you want to create:

 
// Use the member variables to create a non-Modal Dialog Box PTD = new ctestdialog (); // allocate the memory to the pointer PTD-> Create (idd_dialog1 ); // create a non-Modal Dialog Box PTD-> showwindow (sw_shownormal); // display the non-Modal Dialog Box

 

Finally, in the destructor of the class, the memory pointed to by the PTD is reclaimed:

 
Delete PTD;

 

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.