Differences between the MFC Modal Dialog Box and the non-Modal Dialog Box

Source: Internet
Author: User
The main difference between a non-modal dialog box and a modal dialog box is that a non-modal dialog box does not monopolize user input. After a non-modal dialog box is opened, the user can still interact with other interfaces. However, the modal dialog box must be closed before you can operate on other interfaces.

Specific differences include:


1. Non-Modal Dialog Box objects are dynamically created using the new operator in the heap, instead of being embedded into other objects in the form of member variables or built on the stack as 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.


2. The non-modal dialog box is started by calling the cdialog: create function, while the modal dialog box is started by calling the cdialog: domodal function. Since the create function does not start a new message loop, the dialog box and the application share the same message loop, 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.
As we all know, in the MFC program, the lifetime of the window object should be longer than the corresponding window, that is, the corresponding window object cannot be deleted without closing the window on the screen. 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 heap, it cannot be constructed in the form of local variables.


3. The template of the non-modal dialog box must have the visible style. Otherwise, the dialog box is invisible, and the modal dialog box does not need to set this style. The more insurance method is to call cwnd: showwindow (sw_show) to display the dialog box, regardless of whether the dialog box has the visible style.


4. The non-modal dialog box must be closed by calling cwnd: destroywindow instead of cdialog: enddialog. 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. In the non-modal dialog box, delete the dialog box objects using the delete operator. Because the non-Modal Dialog Box object is built using the new operator, you must delete the dialog box object using the delete operator 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 the object itself
}
In this way, after the dialog box is deleted, the dialog box objects are automatically deleted. The owner object does not need to explicitly call Delete to delete the dialog box objects.


6. A flag is required to indicate 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.


Based on the above analysis, we will briefly introduce how to write code.
Cdemoview: cdemoview ()
{
// Todo: Add construction code here
M_pdemodlg = NULL; // pointer Initialization is null
}
 
Void cdemoview: oneditdemo ()
{
// Todo: add your command handler code here
If (m_pdemodlg)
M_pdemodlg-> setactivewindow (); // activation dialog box
Else
{
// Create a non-Modal Dialog Box
M_pdemodlg = new cdemodialog (this );
M_pdemodlg-> Create (idd_demo, this );
}
}
 
Void cdemodialog: postncdestroy ()
{
// Todo: add your specialized code here and/or call the base class
Delete this; // delete a dialog box object
}
 
Void cdemodialog: oncancel ()
{
// Todo: add extra cleanup here
(Cdemoview *) m_pparent)-> m_pdemodlg = NULL;
Destroywindow (); // Delete dialog box
}

Egg ~~~~~~

An MFC window object contains two elements: one is the window object encapsulation window, that is, the hwnd (window handle) stored in the m_hwnd member ), second, the window object itself is a C ++ object. To delete an MFC window object, delete the window object encapsulated by the window object and then delete the window object itself.


The most direct method to delete a window is to call cwnd: destroywindow or: destroywindow. The former encapsulates the latter's functions. The former not only calls the latter, but also invalidates the hwnd saved by m_hwnd (null ). If destroywindow deletes a parent window or owner window, the function automatically deletes all child windows or owner windows before deleting the parent window or owner. In general, you do not need to directly call destroywindow in the program to delete the window, because MFC will automatically call destroywindow to delete the window. For example, when a user exits the application, a wm_close message is generated, which causes the MFC to automatically call cwnd: destroywindow to delete the main framework window, when you press the OK or cancel button in the dialog box, MFC automatically calls cwnd: destroywindow to delete the dialog box and its controls.


The deletion of window objects is divided into two types based on the object creation method. In MFC programming, a large number of window objects are used. Some window objects are embedded in another object as variables or created on the stack as local variables, some are created in the heap using the new operator. For a window object created as a variable, the programmer does not have to worry about its deletion, because the life cycle of the object is always limited. if the object is a member variable of an object, it disappears as the parent object disappears. if the object is a local variable, it is cleared when the function returns. The lifetime of a window object dynamically created in the heap is arbitrary. When beginners are learning C ++ programming, they are often not very practical about the use of the new operator. Because New is used to create an object in the heap, they cannot forget to delete the object with Delete. When learning the MFC routine, the reader may have such a question: why does some programs use new to create a window object without explicitly using Delete to delete it? The answer is that some MFC window objects can be automatically cleared. As mentioned earlier in the non-modal dialog box, when you call cwnd: destroywindow or: destroywindow to delete a window, the postncdestroy member function of the deleted window will be called. The default postncdestroy does nothing, but some MFC window classes will overwrite the function and call Delete this in the new version of postncdestroy to delete the object, thus providing the automatic clearing function. This type of window objects are usually created in the heap with the new operator, but the programmer does not have to worry about using the delete operator to delete them, because once the destroywindow is called to delete the window, the corresponding window object will be deleted immediately.


The window classes that do not have the automatic clearing function are as follows. These window objects are usually created in the form of variables, without the need to automatically clear the function.
All standard Windows Control classes.
Subwindow objects derived directly from the cwnd class (such as custom controls ).
Split Window class csplitterwnd.
The default control bar class (including the toolbar, Status Bar, and dialog bar ).
Modal Dialog Box class.
 
The window classes with the automatic clearing function are as follows. These window objects are usually created in the heap.
Main framework window class (derived directly or indirectly from the cframewnd class ).
View class (derived directly or indirectly from the cview class ).

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.