MFC modal dialog boxes and non-modal dialogs

Source: Internet
Author: User

There are two forms of dialogs in MFC, one modal dialog (Model dialog box) and one non-modal dialog (modeless dialog box).

A modal dialog (Model dialog box)

In the process of running the program, if a modal dialog box appears, the main window will not be able to send the message until the modal dialog box exits before it can be sent.

Click the OK button in the modal dialog box and the modal dialog will be destroyed.

To create a modal dialog box code:

	Create a modal dialog box
	Ctestdialog TD;
	

Where Ctestdialog is my own dialog class that is associated with a new dialog resource.

You can create a layout modal dialog class variable without worrying that it will be destroyed as the function returns. Because one of the functions of the DoModal () function is to run only this modal dialog box at this moment, and to stop the main window from running until the modal dialog box exits, the main window is allowed to run.

The DoModal () function also has the ability to display dialog boxes, so there is no need to call other functions to display the dialog box.

Second, non-modal dialog (modaless dialog box)

In the process of running the program, if a non-modal dialog box appears, the main window can also send messages.

Click on the OK button in the Non-modal dialog box, the non-modal dialog is not destroyed, just hidden. If you want to click the OK button and the Non-modal dialog box is destroyed, the Ctestdialog class must reload the virtual function OnOK () of its base class CDialog and call DestroyWindow () in this function to destroy the dialog box.

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

           Ctestdialog TD;
	Td. Create (IDD_DIALOG1); Creates a non-modal dialog box
	TD. ShowWindow (SW_SHOWNORMAL); Show Non modal dialog box

Then, at run time, you will find that this dialog box cannot be displayed. This is because you declare that the dialog variable TD is a local variable, but when this function returns, TD is also refactored, 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 () that needs to be displayed to display the dialog box. There are two ways to create it:

(1) Creating a non-modal dialog box with local variables

	Create a non-modal dialog box with local variables
	ctestdialog *ptd = new Ctestdialog ();
	Ptd->create (IDD_DIALOG1); Create a non-modal dialog box
	Ptd->showwindow (SW_SHOWNORMAL);//Show Non modal dialog

Because the pointer is placed on the stack at the time of declaration, only the entire application is closed before it is destroyed, so the dialog box can be displayed normally.

This method does not affect the operation of the program, but pointer ptd the memory pointed to is not available, such programming is not good.

(2) Creating a non-modal dialog box with member variables

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

Private:
	Ctestdialog *ptd;

Then add the following code in the appropriate CPP file where you want to create the dialog box:

	Create a non-modal dialog box with member variables
	PTD = new Ctestdialog ();//Assign the pointer memory
	ptd->create (IDD_DIALOG1);//Create a non-modal dialog box
	ptd- >showwindow (SW_SHOWNORMAL); Show Non modal dialog box

Finally, the memory pointed to by the PTD is retracted in the destructor of the class in which it is located:

	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.