Modal Dialog Box and non-modal dialog box in MFC

Source: Internet
Author: User

There are two types of MFC dialog boxes: Modal Dialog Box and modaless dialog box, also called the stateless dialog box ). The difference between the two lies in whether to allow users to operate other objects when the dialog box is opened.

1. Modal Dialog Box

A modal dialog box means that you must close the dialog box before you can edit other dialogs or modules.

Create as follows:

// Create Modal Dialog Box cmodaldialog modaldialog; modaldialog. domodal ();
In the above example, modaldialog is an object in a dialog box. The main difference between a mode and a non-modal mode is that on domodal, when the program is executed to domodal, it will tell the program, currently, only operations in the current dialog box can be performed, and other operations are temporarily stopped. Domodal is not completed until the current dialog box is closed.

2. Non-Modal Dialog Box

The non-modal dialog box indicates that you can still operate other windows when opening the non-modal dialog box. For example, the FIND dialog box in the Notepad program provided by windows. The [Search] dialog box does not monopolize user input. After you open the [Search] dialog box, you can still interact with other user interface objects. Users can search and modify articles, which greatly facilitates use.

To create a non-modal dialog box, you must call the CREATE () and showwindow () functions. When the current dialog box is ended, you must use destroywindow () to forcibly destroy the created dialog box.

Is the following situation feasible?

<PRE name = "code" class = "CPP"> void cyourview: onopendlg (void) {/* assume that idd_test_dlg is the ID number of the defined dialog box resource */cmodalessdialog modalessdialog; modalessdialog. create (idd_test_dlg); modalessdialog. showwindow (sw_show );}
 

During running, you will find that this dialog box cannot be displayed. This is because the declared dialog box variable modalessdialog is a local variable, but modalessdialog is also destructed when this function is returned, so this dialog box cannot be displayed. 

Void cyourview: onopendlg (void) {/* assume that idd_test_dlg is the ID of the defined dialog box resource */cmodalessdialog * modalessdialog = new cmodalessdialog; modalessdialog-> Create (custom) modalessdialog-> showwindow (sw_show); // do not delete modalessdialog to prevent the end object .}


 
Because the undefined object has not been deleted, it always exists, knowing that the program is finished, so this dialog box is always displayed, but it belongs to a pointer and does not release its memory, this will cause unnecessary errors. Therefore, the object can be defined as a member and the requested memory can be released in the destructor. 

You can decide when to destroy the created dialog box. For example, you can choose to forcibly close the dialog box in the onclose () function after clicking the close button. As follows:

void CYourView::OnCancel()    {       CDialog::OnCancel();       DestroyWindow();    }


 
As shown above, the dialog box can be forcibly closed. 


End.


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.