VC ++ modal dialog box and non-modal dialog box, vc Modal Dialog Box

Source: Internet
Author: User

VC ++ modal dialog box and non-modal dialog box, vc Modal Dialog Box

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

The modal dialog box means that when it is displayed, the program will pause the execution until the modal dialog box is closed, and other tasks in the program can continue to be executed. A non-modal dialog box allows you to execute other tasks in the program instead of closing the dialog box when it is displayed.

Create a modal dialog box: To create a modal dialog box, call the member function of the CDialog class: DoModal. The function is to create and display a modal dialog box. The returned value is used as another member function of the CDialog class: endDialog parameter. The latter function is to close the modal dialog box. The implementation code of the show modal dialog box is as follows:

void CASCEView::OnDialog() {         CASCEDlg dlg;          dlg.DoModal(); }

Create a non-Modal Dialog Box: To Create a non-modal dialog box, you need to use the Create member function of the CDialog class. This function has the following two forms of declaration:

virtual BOOL Create(    LPCTSTR lpszTemplateName,    CWnd* pParentWnd = NULL ); virtual BOOL Create(    UINT nIDTemplate,    CWnd* pParentWnd = NULL );

It can be seen from the above that the first parameter of the CDialog: Create function can be the ID of the dialog box resource (nIDTemplate), or the name of the dialog box template (lpszTemplateName ); the second parameter specifies the parent window of the dialog box. If the value is NULL, the parent window of the dialog box is the main application window.

When the Create function is used to Create a non-modal dialog box, we also need to call the ShowWindow function to display this dialog box. The modal dialog box created by DoModal is not used, it is because the DoModal function itself has the function of displaying the modal dialog box. At the same time, the dialog box cannot be defined as an object like the modal dialog box. The following code does not display the non-modal dialog box:

void CASCEView::OnDialog() {          CASCEDlg dlg;          dlg.Create(IDD_DIALOG1, this);          dlg.ShowWindow(SW_SHOW); }

Because the non-Modal Dialog Box object dlg created here is a local object, each code is executed in sequence when the program is executed. When the OnDialog function is executed, the lifecycle of the dlg object is also fun, and it will destroy the associated dialog box resources. The dialog box will naturally not be displayed! The modal dialog box can be displayed because when the DoModal function is called to display the modal dialog box, the program will pause the execution until the modal dialog box is closed. Before that, the dlg has not been destroyed.

Therefore, when creating a non-modal dialog box, you cannot define the dialog box object as a local variable. solution 2: Define the dialog box object as a member variable of the CASCEView class; second, define it as a pointer to allocate memory on the heap, as shown below:

void CASCEView::OnDialog() {          CASCEDlg *pDlg = new CASCEDlg;          pDlg->Create(IDD_DIALOG1, this);          pDlg->ShowWindow(SW_SHOW); }

However, this introduces a new problem: we must release resources occupied by pDlg, otherwise it will cause memory leakage! In addition, pDlg is also a local pointer variable. When its lifecycle ends, it will no longer be able to reference the memory it points to in the program. There are two solutions: 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 release the memory it points; the second is to reload the PostNcDestroy virtual function in the CASCEDlg class to release the memory pointed to by this pointer:

void CASCEDlg::PostNcDestroy() {          delete this;          CDialog::PostNcDestroy(); }

Note that: When you click the default OK button on the dialog box, both the dialog boxes will disappear. But for the modal dialog box, the dialog box window object is destroyed. For the non-modal dialog box, the dialog box window object is not destroyed, but hidden.

After clicking the OK button in the non-modal dialog box, the program will call the OnOK function of the base class CDialog. This is a virtual function, and the latter will call the EndDialog function. This function is used to terminate the modal dialog box, for a non-modal dialog box, this function only makes the dialog box window invisible and does not destroy it. Therefore, for the non-modal dialog box, if there is a button with the ID value of IDOK, you must override the OnOK virtual function of the base class and call the DestroyWindow function in the rewritten function, to complete the destruction dialog box, And do not call the OnOK function of the base class. Similarly, if the non-modal dialog box contains a button whose ID value is IDCANCEL, you must also override the OnCancel virtual function of the base class and call the DestroyWindow function in the rewritten function to destroy the dialog box, do not call the OnCancel function of the base class any more.

The creation and destruction process of a non-Modal Dialog Box differs from that of a modal dialog box.

Let's take a look at the original article of MSDN: When you implement a modeless dialog box, always override the OnCancel member function and call DestroyWindow from within it. don't call the base class CDialog: OnCancel, because it callenddialog, which will make the dialog box invisible but will not destroy it. you shoshould also override PostNcDestroy for modeless dialog boxes in order to delete this, since modeless dialog boxes are u Sually allocated with new. modal dialog boxes are usually constructed on the frame and do not need PostNcDestroy cleanup. MS instructions: the non-modal dialog box needs to overload the OnCanel function and call DestroyWindow in this function. The OnCancel of the base class cannot be called because the OnCancel of the base class calls the EndDialog function, which is for the modal dialog box.

Another function that must be overloaded is PostNcDestroy, which is also a virtual function. The non-modal dialog box is usually created using a class pointer through new, in this case, delete the pointer in the PostNcDestroy function.

After learning about the theory, we can use the code to create and destroy a non-modal dialog box:

// Create

// In the main framework:

CTestDlg *pDlg=new CTestDlg; pDlg->Create(IDD_TESTDLG,this); pDlg->ShowWindow(SW_SHOW);

// In the dialog box:

void CTestDlg::OnCancel() {      DestroyWindow(); } void CTestDlg::PostNcDestroy() {      CDialog::PostNcDestroy();      delete this; }
To destroy the non-modal dialog box when you click the button, you only need to map the event of the button to the OnCancel function.

The following is a reference:

The order in which messages are processed in the MFC Application

1. AfxWndProc () This function is used to receive messages, locate the CWnd object to which the message belongs, and then call AfxCallWndProc.

2. AfxCallWndProc () This function is used to save messages (Message identifiers and message parameters are mainly saved) for future use by the application, and then calls the WindowProc () function.

3. WindowProc () this function is responsible for sending messages to the OnWndMsg () function. If not processed, call the DefWindowProc () function.

4. OnWndMsg () The function first sorts messages by byte. For WM_COMMAND messages, the OnCommand () Message response function is called, and the OnNotify () Message response function is called for WM_NOTIFY messages. Any missed message is a window message. OnWndMsg () function searches for a message image to find a processing function that can process messages in any window. If the OnWndMsg () function cannot find such a processing function, the message is returned to the WindowProc () function, which sends the message to the DefWindowProc () function.

5. onCommand () This function checks whether this is a control notification (the lParam parameter is not NULL. If the lParam parameter is NULL, the message is not a control notification). If it is, OnCommand () the function tries to map messages to the control that creates notifications. If it is not a control notification (or if the control rejects the mapped message) OnCommand (), it calls the on1_msg () function.

6. Based on the class that receives messages, the onreceivmsg () function potentially transmits Command messages and control notifications in 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 transmitted to the view and document class, and a message processing function is found for the class.

The process of creating an MFC application window

1. PreCreateWindow () this function is an overload function. Before a window is created, you can change the creation parameters in this overload function (you can set the window style and so on)

2. PreSubclassWindow (), which is also an overload function that allows subclassification of a window.

3. OnGetMinMaxInfo () this function is a message response function that responds to WM_GETMINMAXINFO messages and allows you to set the maximum or minimum size of the window.

4. OnNcCreate () this function is also a message response function. It responds to the WM_NCCREATE message and sends a message to inform the customer zone of the window that will be created soon.

5. OnNcCalcSize () this function is also a message response function. It responds to the WM_NCCALCSIZE message and allows you to change the size of the window client.

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

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

8. The OnMove () Message response function responds to the WM_MOVE message. sending this message indicates that the window is moving

9. OnChildNotify () this function is an overloaded function and is called as part of message ing, telling the parent window that a window has just been created

The order in which the MFC application closes the window (non-modal window)

1. OnClose () Message response function. The WM_CLOSE message in the response window is sent when the close button is clicked.

2. OnDestroy () Message response function, which refers to the WM_DESTROY message in the response window. When a window is destroyed, this message is sent.

3. OnNcDestroy () Message response function, which refers to the WM_NCDESTROY message in the response window. This message is sent when a window is destroyed.

4. The PostNcDestroy () overload function is called by CWnd as the final action for processing the OnNcDestroy () function.

Order of function calls in the open mode dialog box in the MFC Application

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

2. The PreSubclassWindow () overload function allows users to classify a window first.

3. The OnCreate () Message response function responds to the WM_CREATE message and sends this message to indicate that a window has been created.

4. The OnSize () Message response function responds to the WM_SIZE message and sends the message to notify the window size of the change.

5. The OnMove () Message response function responds to the WM_MOVE message and sends the message to notify the window that it is moving.

6. The OnSetFont () Message response function responds to the WM_SETFONT message and sends the message to allow the font of the control in the dialog box to be changed.

7. The OnInitDialog () Message response function responds to the WM_INITDIALOG message and sends the message to allow the control in the initialization dialog box or create a new control.

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

9. The OnCtlColor () Message response function responds to the WM_CTLCOLOR message. The color of the control in the changed dialog box or dialog box is sent by the parent window.

10. The OnChildNotify () overload function is sent as the result of the WM_CTLCOLOR message.

Order of the close mode dialog box in the MFC Application

1. The OnClose () Message response function responds to the WM_CLOSE message. When the "close" button is clicked, the function is called.

2. The OnKillFocus () Message response function responds to the WM_KILLFOCUS message. It is sent when a window is about to lose the keyboard input focus.

3. The OnDestroy () Message response function responds to the WM_DESTROY message. When a window is about to be destroyed, it is sent.

4. The OnNcDestroy () Message response function responds to the WM_NCDESTROY message and is sent after a window is destroyed.

5. The PostNcDestroy () overload function is called by CWnd as the final action for processing the OnNcDestroy () function.

Order of opening the modeless dialog box

1. The PreSubclassWindow () overload function allows users to subclassify a window first

2. The OnCreate () Message response function responds to the WM_CREATE message and sends this message to indicate that a window has been created.

3. The OnSize () Message response function responds to the WM_SIZE message and sends the message to notify the window size of the change.

4. The OnMove () Message response function responds to the WM_MOVE message and sends the message to notify the window that it is moving.

5. The OnSetFont () Message response function responds to the WM_SETFONT message and sends this message to allow the font of the control in the dialog box to be changed.

All the preceding operations are performed in the given order!

Related Article

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.