From: http://hi.baidu.com/asd4790007/item/8a1a71e01ba12b15595dd8da
First, OnOK () and OnCancel () are member functions of the CDialog base class, while OnClose () and OnDestroy () are member functions of the CWnd base class, that is, WM message response functions. From the perspective of the application structure, taking a dialog box, the red X corresponds to CWnd, and the "OK" and "Cancel" buttons in the dialog box correspond to CDialog.
Second, OnClose () and OnDestroy ()
In a single-view program, according to << Dive Into MFC >>, the sequence of operations performed when the program exits is (from the point X button)
(1) The user clicks the X exit button and sends a WM_CLOSE message -----> Response OnClose ()
(2) In the WM_CLOSE message processing function, call DestroyWindow () -----> destroy the window associated with the specified CWnd window object, but the CWnd object is not destroyed
(3) WM_DESTROY message was sent in DestroyWindow () -----> Response to OnDestroy () after window destruction
(4) Call PostQuitMessage () in the WM_DESTROY message, send the WM_QUIT message, and end the message loop
It can be seen that the exit process of the program first responds to OnClose () and then to OnDestroy (). Before responding to OnDestroy (), the window object has been destroyed. What exactly does OnDestroy () do? It is like a teller. It first informs the CWnd object that it is about to be destroyed. Then the actual operation of OnDestroy is called after the CWnd object has been cleared from the screen.
Third, OnOK (), OnCancel () (), OnClose (), OnDestroy ()
CDialog :: OnOK first calls UpdateData (TRUE) to pass data to the dialog member variables, and then calls CDialog :: EndDialog to close the dialog;
CDialog :: OnCancel only calls CDialog :: EndDialog to close the dialog box;
OnClose () responds to WM_CLOSE. To a certain extent, it can be said that CDialog :: EndDialog () and OnClose () accomplish similar tasks, but the processing mechanism is different. The former is the CDialog object mechanism and the latter is the WM message mapping mechanism. .
CDialog :: EndDialog () --------> OnDestroy ()
OnClose () --------> OnDestroy ()
EndDialog () and OnClose () belong to the same level, so when we press the OK button, the program will not execute OnClose (), but both mechanisms must go through OnDestroy ()
Then found
The following two statements are not clear
(1) EndDialog (-1);
Closes the modal dialog and returns the parameter as the return value of the parent dialog call.
(2) DestroyWindow (); :: PostQuitMessage (0);
DestroyWindow closes the modeless dialog box. Exit the message loop and actually end the process. There are many program windows closed, but this does not mean exiting.
The difference between cDialog :: onok (), enddialog (), destroywindow. Favorite
The termination of modal and modeless dialogs is different: modal dialogs are terminated by calling CDialog :: EndDialog, modeless dialogs are terminated by calling CWnd :: DestroyWindow, and functions CDialog :: OnOK and CDialog :: OnCancel call EndDialog, So you need to call DestroyWindow and reset the modeless dialog function.
The last three are not bad
Windows API practice one day (18) EndDialog function
The function of displaying a dialog box was introduced last time, so how to close the dialog box? This requires the use of the function EndDialog. This function can only be used in the message processing function of the dialog box, and after the function is called, the dialog box is not deleted immediately, but the end flag in the operating system is set. When the operating system detects this flag, it deletes the message loop of the dialog box and releases the resources occupied by the dialog box. In fact, the life cycle of the dialog box is like this. The dialog box is first created by the function DialogBox. In this case, the function DialogBox will issue a message WM_INITDIALOG before the dialog box is created. Set the text string, etc. Finally, when the user clicks the OK or Cancel button, he receives two commands IDOK or IDCANCEL, and then he can call the function EndDialog to end the life of the dialog box.
The function EndDialog is declared as follows:
WINUSERAPI
BOOL
WINAPI
EndDialog (
__in HWND hDlg,
__in INT_PTR nResult);
hDlg is the handle of the dialog window.
nResult is the return value set to the function DialogBox.
An example of calling this function is as follows:
# 001 // Show about dialog.
# 002 //
# 003 // Cai Junsheng 2007/07/12
# 004 //
# 005 INT_PTR CALLBACK About (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
# 006 {
# 007 UNREFERENCED_PARAMETER (lParam);
# 008 switch (message)
# 009 {
# 010 case WM_INITDIALOG:
# 011 return (INT_PTR) TRUE;
# 012
# 013 case WM_COMMAND:
# 014 if (LOWORD (wParam) == IDOK || LOWORD (wParam) == IDCANCEL)
# 015 {
# 016 EndDialog (hDlg, LOWORD (wParam));
# 017 return (INT_PTR) TRUE;
# 018}
# 019 break;
# 020}
# 021 return (INT_PTR) FALSE;
# 022}
Line 16 is to call the function EndDialog to close the dialog box.
Contact: Manager Xu
Phone: 13907330718
Tel: 0731-22222718
Email: hniatcom@163.com
Add: Room 603, 6th Floor, Shifting Room, No. 2, Orbit Zhigu, No. 79 Liancheng Road, Shifeng District, Zhuzhou City, Hunan Province