Differences between a modal dialog box and a non-Modal Dialog Box

Source: Internet
Author: User

Assume that the dialog box class is ctestdlg.
1. For the modal dialog box:

Ctestdlg CTD; // create this dialog box
CTD. domodale ();

2. If it is not modal:

Ctestdlg * p_ctd = new ctestdlg (); // object pointer in the create dialog box
P_ctd-> Create (idd_reading, this); // the pointer calls the CREATE () function. The ID number is in the dialog box.
P_ctd-> showwindow (sw_show); // The parameters in () can be used to change the display mode of the dialog box (for details, see the showwindow () function in msdn)

 

Differences between the two:

1. The template of the non-modal dialog box must have the visible style (visible = true); otherwise, the dialog box will not be visible, and the modal dialog box does not need to set this style. In actual programming, the more safe way is to callCwnd: showwindow (sw_show)To display the dialog box, regardless of whether the dialog box has the visible style.

2. Non-Modal Dialog Box objects are dynamically created using the new operator, rather than being embedded into other objects in the form of member variables or constructed in the form of local variables. Generally, a pointer member variable pointing to the dialog box class should be declared in the dialog box owner's window class, through which the dialog box object can be accessed.

3. CallCdialog: CreateFunction to start the dialog box, insteadCdialog: domodalThis is the key to the difference between the two. Since the create function does not start a new message loop, the dialog box shares the same message loop with the application, so that the dialog box will not monopolize user input. Create returns the result immediately after the dialog box is displayed, while domodal returns the result only after the dialog box is closed. After the return result of create, you cannot determine whether the dialog box is closed or not, so you cannot determine the lifetime of the dialog box object. Therefore, you have to build the dialog box object in the stack, it cannot be constructed in the form of local variables.

4. required callsCwnd: destroywindowInsteadCdialog: enddialogTo close the non-modal dialog box. Calling cwnd: destroywindow is a common method for directly deleting a window. Because the default cdialog: onok and cdialog: oncancel functions call enddialog, the programmer must write their own onok and oncancel functions and call destroywindow in the function to close the dialog box.

5. Because the non-Modal Dialog Box object is built using the new operator, you must use the delete operator to delete the dialog box object after the dialog box is closed. After a window is deleted on the screen, the Framework calls cwnd: postncdestroy, which is a virtual function. The program can delete window objects in this function. The Code is as follows:
Void cmodelessdialog: postncdestroy
{Delete this; // Delete object}
In this way, after the dialog box is deleted, the dialog box objects are automatically deleted. The owner does not need to explicitly call Delete to delete the dialog box objects.

6. There must be a flag indicating whether the non-modal dialog box is opened. The reason for this is that you may choose to open the command again when opening a modal dialog box. The program determines whether to open a new dialog box or simply activate the original Dialog Box Based on the flag. Generally, the pointer to the dialog box object in the owner window can be used as this flag. When the dialog box is closed, a null value is assigned to the pointer to indicate that the dialog box object does not exist.

 

Void ctestdlg: onbutton1 ()
{
// Todo: add your control notification handler code here
Cstring STR;
Str. Format ("dialog box handle % d", getsafehwnd ());
MessageBox (STR );
}

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.