The differences between the MFC modal dialog box and the Non modal dialog box

Source: Internet
Author: User

The dialog box is divided into two main types: modal dialog and non-modal dialog box.

1. The modal dialog box features:
A) The background window cannot be manipulated until the modal dialog box is closed, unless it is the main window itself;
b) using CDialog::D the Omodal function popup Mode dialog box, the function execution process is a blocking function;
c) Therefore, in the stack or in the heap to apply for the dialog box class object can be, the application stack internal variable development and run more efficient.

2, non-modal dialog box features are:
A) You can manipulate the background window while the non-modal dialog is running;
b) using the Cdialog::create function and the CWnd::ShowWindow pop-up modal dialog box, the function is non-blocking function execution procedure;
c) You cannot request a temporary object for a dialog class within the stack, you need to request a variable in the heap or use a long-life object, such as global variables, member variables, and so on.
D) After calling the EndDialog function, only the window is hidden, not the real is closed, the WM_DESTROY message will not be recalled;
e) If you want to actually close the modeless dialog box, you need to call CWnd::D Estroywindow function;

The difference between the two:
I. The template for the non-modal dialog must have a visible style (visible=true), otherwise the dialog box will not be visible, and the modal dialog box does not have to be set to that style.
The more insurable approach in practical programming is to call CWnd::ShowWindow (Sw_show) To display the dialog box, regardless of whether the dialog box has a visible style.
Two. The non-modal dialog object is created dynamically with the new operator instead of being embedded in another object as a member variable or in the form of a local variable.
You typically declare a pointer member variable in the owner window Class of a dialog box to a dialog class that accesses the dialog box object.
Three. Start the dialog box by calling the Cdialog::create function instead of CDialog::D Omodal, which is the key to the difference between the two.
Because the CREATE function does not start a new message loop, the dialog box uses the same message loop as the application, so that the dialog box does not monopolize user input.
Create returns immediately after the dialog box is displayed, and DoModal is returned when the dialog box is closed. Because after the Create is returned, you cannot determine whether the dialog box is closed.
This also makes it impossible to determine the lifetime of the dialog object, so you have to build the dialog object in the stack, not in the form of a local variable.
Four. CWnd must be called::D Estroywindow instead of Cdialog::enddialog to close the Non modal dialog box. Call CWnd::D Estroywindow is a general way to delete a window directly.
Because the default Cdialog::onok and Cdialog::oncancel functions call EndDialog, programmers must write their own OnOK and OnCancel functions and invoke them in the function.
DestroyWindow to close the dialog box.
Five. Because the non-modal dialog object is constructed with the new operator, you must delete the dialog object with the Delete operator after the dialog box is closed. After a window is deleted on the screen,
The framework calls CWnd::P Ostncdestroy, which is a virtual function in which the program can complete the task of deleting a Window object, with the following code
void Cmodelessdialog::P Ostncdestroy
{Delete this; Delete Object}
This allows the dialog box object to be automatically deleted when the dialog box on the screen is deleted. The owner does not have to explicitly call Delete to delete the dialog object.
Six. There must be a flag indicating whether the non-modal dialog box is open. The reason for this is that the user may choose to open the command again, in the case of opening a modal dialog box.
The program depends on the flag to open a new dialog box, or simply to activate the previously opened dialog box. This flag can usually be used as a pointer to a dialog object in the Owner's window,
When the dialog box is closed, assign a null value to the pointer to indicate that the dialog object no longer exists.
For example:
Create modal dialog box
Ctestdlg Dlg;
Dlg. DoModal ();
To create a non-modal dialog box
Ctestdlg * dlg = new Ctestdlg;
Dlg->create (IDD_TEST_DLG);
Dlg->showwindow (Sw_show);

There must be a flag indicating whether the non-modal dialog box is open. The reason for this is that the user may choose to open the command again, in the case of opening a modal dialog box.
Make Notepad's Find dialog box as follows:

BOOL iscreate;//Flag Whether a Find dialog box is already open
cfinddlg* Pfinddlg;

void Cnotepaddlg::oneditfind () {if (iscreate = = FALSE) {  Pfinddlg = new Cfinddlg;pfinddlg->create (idd_find_ DIALOG); iscreate = True;pfinddlg->showwindow (Sw_show);}}


This way, when you click to open the Find dialog, you will not generate several dialogs in succession!

CWnd must be called::D Estroywindow instead of Cdialog::enddialog to close the Non modal dialog box. Call CWnd::D Estroywindow is a general way to delete a window directly.
Because the default Cdialog::onok and Cdialog::oncancel functions call EndDialog, programmers must write their own OnOK and OnCancel functions and invoke them in the function.
DestroyWindow to close the dialog box.

Non-modal dialog box, if we call the EndDialog function to close the dialog box, it is not destroyed, the knowledge is hidden, when we click Find again, the Find dialog box appears

It does not regenerate, but instead displays the original generated, for a modeless dialog box, and the EndDialog function is equivalent to hiding the effect of the dialog Hidedialog.

The differences between the MFC modal dialog box and the Non modal dialog box

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.