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:
1 // Creating a modal dialog box 2 Ctestdialog TD; 3
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:
1 Ctestdialog TD; 2 // creating a non-modal dialog box 3 // 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
1 // Create a non-modal dialog box with local variables 2 New Ctestdialog (); 3 // creating a non-modal dialog box 4 // Show Non modal dialog box
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:
1 Private : 2 Ctestdialog *ptd;
Then add the following code in the appropriate CPP file where you want to create the dialog box:
1 // Create a non-modal dialog box with member variables 2 New // allocating memory to pointers 3 // creating a non-modal dialog box 4 // 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:
1 Delete PTD;
CSDN Original: Candyliuxj http://blog.csdn.net/candyliuxj/article/details/6736032
"Go" MFC modal dialog box and non modal dialog box