MFC method to create modal and modeless dialogs

  • 2020-04-02 02:30:25
  • OfStack

There are two forms of dialog in MFC, the model dialog box and the modeless dialog box. This article briefly describes how to create them respectively.

1. Model dialog box

In the process of the program running, if there is a modal dialog box, then the main window will not be able to send messages, until the modal dialog box exit can be sent.
Click the OK button in the modal dialog box and the modal dialog box will be destroyed.
The code to create a modal dialog is as follows:


//Create a modal dialog box
CTestDialog td;
td.DoModal(); 

Where CTestDialog is my new dialog class associated with a dialog resource.
You can create a layout modal dialog class variable without worrying that it will be destroyed when the function returns. Because one of the functions of the DoModal() function is that you can only run the modal dialog box at this time, and stop the main window until the modal dialog box exits.
The DoModal() function also has the ability to display the dialog box, so there is no need to call other functions to display the dialog box.

Ii. Modaless dialog box

The main window can also send a message if a modeless dialog box appears while the program is running.
Click the OK button in the modeless dialog box, the modeless dialog box is not destroyed, it is just hidden. If the modeless dialog box is also destroyed when the OK button is clicked, the CTestDialog class must override the virtual function OnOK() of its base CDialog class, which calls DestroyWindow() to destroy the dialog box.

Here we create a modeless dialog box in the same way as above. The code is as follows:


CTestDialog td;
td.Create(IDD_DIALOG1); //Create a modeless dialog box
td.ShowWindow(SW_SHOWNORMAL); //Displays the modeless dialog box

Then, at run time, you will find that the dialog box is not displayed. This is because the dialog variable td that you declared is a local variable, but when this function returns, td is also destructed, so the dialog cannot be displayed.

To create a modeless dialog, you must declare a pointer to the CTestDialog class, and the displayed call to ShowWindow() is required to display the dialog. There are two ways to create it:

(1) use local variables to create a modeless dialog box


//Create a modeless dialog with a local variable
CTestDialog *pTD = new CTestDialog();
pTD->Create(IDD_DIALOG1); //Create a modeless dialog box
pTD->ShowWindow(SW_SHOWNORMAL); //Displays the modeless dialog box

Because the pointer is placed on the stack at the time of the declaration and is destroyed only when the entire application is closed, the dialog box can be displayed normally.
Although this method does not affect the program to run, but the pTD pointer to the memory is not available, which is not good programming.

(2) use the member variable to create a modeless dialog box
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 in the location where you want to create the dialog box:


//Create a modeless dialog with a member variable
pTD = new CTestDialog(); //Allocate memory to Pointers
pTD->Create(IDD_DIALOG1); //Create a modeless dialog box
pTD->ShowWindow(SW_SHOWNORMAL); //Displays the modeless dialog box

Finally, recover the memory pointed to by pTD in the destructor of the class:


delete pTD;

Related articles: