VC + + modal dialog box and non modal dialog box

Source: Internet
Author: User

There are two types of dialogs in MFC: Modal dialog boxes and non-modal dialogs.

Modal dialogs mean that when they are displayed, the program pauses execution until the modal dialog box is closed before other tasks in the program can continue. The non-modal dialog box refers to allowing other tasks in the program to be executed instead of closing the dialog box when it is displayed.

modal Dialog Creation : Creating modal dialogs requires calling member functions of the CDialog class: DoModal, the function is to create and display a modal dialog box whose return value will act as another member function of the CDialog class: the EndDialog parameter, The function of the latter is to close the modal dialog box. The implementation code for the General Display modal dialog box is as follows:

{         cascedlg dlg;           }

non-modal dialog creation : To create a non-modal dialog box, you need to take advantage of the CDialog class's create member function, which has the following two forms of declaration:

Virtual BOOL Create (    lpctstr lpsztemplatename,    CWnd* pparentwnd =virtual  BOOL Create (    UINT nIDTemplate,    CWnd* pParentWnd = NULL);

From the above, the first argument of the Cdialog::create function can be the ID of the dialog resource (nidtemplate), or it can be the name of the dialog template (Lpsztemplatename), and the second parameter specifies the parent window of the dialog box. If its value is null, the parent window of the dialog box is the main application window.

When creating a non-modal dialog box with the CREATE function, we also need to call the ShowWindow function to display the dialog box, whereas modal dialogs created with DoModal are not used because the DoModal function itself has the function of displaying modal dialogs. At the same time, we cannot define a dialog box as an object, as in modal dialogs, and the following code does not show the non modal dialog box:

void Casceview::ondialog () {          Cascedlg dlg;            This );          Dlg. ShowWindow (Sw_show); }

Because the non-modal dialog object that is created here is a local object, and when the program executes, it executes each code in turn, and when the Ondialog function finishes, the life cycle of the Dlg object is played, and it destroys the dialog resource associated with it, and the dialog box naturally shows no more! Modal dialogs can be displayed because the program pauses execution until the modal dialog is called to display the modal dialog box, and the program continues to execute until it closes DoModal. Before this, the DLG has not been destroyed.

Therefore, when you create a non-modal dialog box, you cannot define a dialog object as a local variable, and the solution is two: one is to define the dialog object as a member variable of the Casceview class, and the other is to define it as a pointer to allocate memory on the heap as follows:

void Casceview::ondialog () {          new  Cascedlg;          Pdlgthis);          Pdlg-ShowWindow (sw_show);}

But this introduces a new problem: we must release the resources that Pdlg consumes, or it will cause a memory leak! Besides, here Pdlg is still a local pointer variable, and when its life cycle ends, it can no longer reference the memory it points to in the program. There are also two workarounds: one is to define PDLG as a member variable of the Casceview class, and then call the delete function in the destructor of the Casceview class to free the memory it points to, and the other is to overload the PostNcDestroy virtual function in the Cascedlg class. Release the memory that this pointer points to:

void Cascedlg::P Ostncdestroy () {          deletethis;          CDialog::P Ostncdestroy (); }

Another thing to note is that when you click the default OK button on the dialog box, both dialog boxes will disappear. For modal dialogs, however, the dialog window object is destroyed, and for non-modal dialogs, the dialog window object is not destroyed, just hidden.

When you click the OK button in the Non-modal dialog box, the program calls the OnOK function of the base class CDialog, which is a virtual function that calls the EndDialog function, which terminates the modal dialog box, but for the non-modal dialog box, the function simply makes the dialog window invisible and does not destroy it. Therefore, for a non-modal dialog box, if you have a button with an ID value of IDOK, you must override the base class's OnOK virtual function and call the DestroyWindow function in the overridden function to complete the task of destroying the dialog box, taking care not to invoke the OnOK function of the base class. Similarly, if you have a button with an ID value of IDCANCEL in the Non-modal dialog box, you must override the base class's OnCancel virtual function, call the DestroyWindow function in the overridden function, destroy the dialog box, and be careful not to call the OnCancel function of the base class again.

The non-modal dialog box differs from the modal dialog box by its creation and destruction process and modal dialogs.

Let's take a look at the original MSDN: when you implement a modeless dialog box, alwaysOverrideThe OnCancel member function and call DestroyWindow fromWithin it. Don ' t call theBase   classCdialog::oncancel, because it calls EndDialog, which would make the dialog box invisible but would   not destroy it. You should alsoOverridePostNcDestroy forModeless dialog boxesinchOrder toDelete    This, since modeless dialog boxes is usually allocated withNew. Modal dialog boxes is usually constructed on the frame and DoNot need PostNcDestroy cleanup. Indication of MS: the non-modal dialog needs to overload the function Oncanel and call DestroyWindow in this function. It is not possible to call the base class's OnCancel, because the oncancel of the base class calls the EndDialog function, which is for modal dialogs. 

There is also a function that must be overloaded is PostNcDestroy, which is also a virtual function, the usual non-modal dialog box is a pointer to the class, created through new, which needs to delete the pointer in the PostNcDestroy function.

After understanding the theory, we can use the code to implement the non-modal dialog box creation and destruction process:

Establish

In the main frame:

Ctestdlg *pdlg=new  Ctestdlg; Pdlg->create (Idd_testdlg,this); Pdlg ShowWindow (Sw_show);

dialog box:

void Ctestdlg::oncancel () {      void  Ctestdlg::P Ostncdestroy () {      CDialog::P Ostncdestroy ();       Deletethis;}
If you want to destroy the non-modal dialog box with the click of a button, simply map the event of the button to the OnCancel function.

Here is a little information for reference:

The order in which messages are processed in an MFC application

1.AfxWndProc () This function is responsible for receiving messages, finding the CWnd object to which the message belongs, and then calling Afxcallwndproc

2.AfxCallWndProc () The function is responsible for saving the message (the content of the message is primarily the information identifier and message parameters) for later use by the application, and then calling the WindowProc () function

3.WindowProc () This function is responsible for sending the message to the Onwndmsg () function, and if it is not processed, call the DefWindowProc () function

4.ONWNDMSG () The function's function first sorts the message by byte, and for the WM_COMMAND message, calls the OnCommand () message response function, and calls the OnNotify () message response function for the WM_NOTIFY message. Any missed messages will be a window message. The onwndmsg () function searches the message image of the class to find a handler function that can handle any window message. If the onwndmsg () function cannot find such a handler function, it returns the message to the WindowProc () function, which sends the message to the DefWindowProc () function

5.OnCommand () This function looks at whether this is a control notification (the LPARAM parameter is not NULL, if the lparam parameter is empty, the message is not a control notification), and if it is, the OnCommand () function attempts to map the message to the control that manufactures the notification ; If he is not a control notification (or if the control rejects the mapped message) OnCommand () calls the OnCmdMsg () function

6.ONCMDMSG () Depending on the class that receives the message, the ONCMDMSG () function will potentially pass command messages and control notifications during a process called command Routing. For example, if the class that owns the window is a framework class, the command and notification messages are also passed to the view and document classes, and a message handler function is found for the class

Procedures for creating Windows for MFC applications

1.PreCreateWindow () This function is an overloaded function, you can change the creation parameters in the overloaded function before the window is created (you can set the window style, etc.)

2.PreSubclassWindow () This is also an overloaded function that allows you to first subclass a window

3.OnGetMinMaxInfo () This function is a message response function, in response to a WM_GETMINMAXINFO message, allowing the setting of the maximum or minimum size of the window

4.OnNcCreate () The function is also a message response function that responds to the Wm_nccreate message, sending a message to tell the window that the client area is about to be created

5.OnNcCalcSize () This function is also a message response function that responds to the wm_nccalcsize message, which is allowed to change the window client area size

6.OnCreate () The function is also a message response function that responds to the WM_CREATE message and sends a message telling a window has been created

7.OnSize () The function is also a message response function that responds to the WM_SIZE message and sends the message to tell the window that the size has changed

The 8.OnMove () message response function, which responds to the Wm_move message, sends this message stating that the window is moving

9.OnChildNotify () The function is an overloaded function, called as part of a message map, telling the parent that the window is about to be told that a window has just been created

The order of the MFC application to close the window (non-modal window)

1.OnClose () message response function, Response window Wm_close message, send this message when the Close button is clicked

2.OnDestroy () message response function, Response window Wm_destroy message, when a window will be destroyed, send this message

3.OnNcDestroy () message response function, Response window Wm_ncdestroy message, when a window is destroyed after sending this message

4.PostNcDestroy () overloaded function, called by CWnd as the last action to handle the OnNcDestroy () function

The sequence of function calls in an MFC application that opens a modal dialog box

1.DoModal () overloaded function, overloaded DoModal () member function

2.PreSubclassWindow () overloaded function, which allows a window to be classified first

3.OnCreate () message response function, in response to a WM_CREATE message, sends this message to tell a window that it has been created

4.OnSize () message response function, in response to the WM_SIZE message, sends this message to tell the window that the size of the change

The 5.OnMove () message response function, which responds to the Wm_move message, sends this message to tell the window that it is moving

6.OnSetFont () message response function, which responds to the Wm_setfont message, sends this message to allow changes to the font of the control in the dialog box

7.OnInitDialog () message response function, which responds to the WM_INITDIALOG message, sends this message to allow the control to initialize the dialog box, or to create a new control

The 8.OnShowWindow () message response function, which responds to the Wm_showwindow message, is called by the ShowWindow () function

9.OnCtlColor () message response function, Response Wm_ctlcolor message, is sent by the parent window the color of the control above the dialog box or dialog box

OnChildNotify () overloaded function, sent as a result of the WM_CTLCOLOR message

Order of closing modal dialogs in MFC applications

The 1.OnClose () message response function, which responds to the WM_CLOSE message, is called when the Close button is clicked.

2.OnKillFocus () message response function, Response Wm_killfocus message, when a window is about to lose keyboard input focus before being sent

3.OnDestroy () message response function, in response to a WM_DESTROY message, is sent when a window is about to be destroyed

4.OnNcDestroy () message response function, in response to a WM_NCDESTROY message, is sent when a window is destroyed

5.PostNcDestroy () overloaded function, called by CWnd as the last action to handle the OnNcDestroy () function

Order of open modeless dialog boxes

1.PreSubclassWindow () overloaded function that allows the user to first sub-categorize a window

2.OnCreate () message response function, in response to a WM_CREATE message, sends this message to tell a window that it has been created

3.OnSize () message response function, in response to the WM_SIZE message, sends this message to tell the window that the size of the change

4.OnMove () message response function, in response to a wm_move message, sends this message to tell the window that it is moving

5.OnSetFont () message response function, which responds to the Wm_setfont message, sends this message to allow changes to the font of the control in the dialog box

All of these executions are performed in a given order!

VC + + modal dialog box and 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.