Window closing process
-- Onok (), oncancel (), onclose (), enddialog (), destroywindow (), ondestroy (), onncdestroy ()
I found that I was misleading and said it was inaccurate. modify it.
1. For non-modal windows, the oncancel function must be reloaded. The destroywindows () method is called in the function, and the base class function cannot be called. Because the base class function calls the enddialog () method. Onclose () also calls the oncancel () method. In addition, to close the dialog box through onok, you must do the same. You cannot use the default method directly.
1. Only the cross icon in the title bar, right-click on the desktop taskbar-Close, ALT + F4, and click-close on the leftmost icon of the title bar to send the wm_close message and trigger onclose ().
Therefore, the process of closing a non-modal window is as follows:
Onclose ()-> oncancel ()-> destroywindow ()-> ondestroy ()-> onncdestroy (),-> only indicates the time sequence.
Onncdestroy () finally calls postncdestroy ()
2. Do not call onclose () when you press enter, ESC, click "OK" or "cancel ()
Generally, onok is the response to id_ OK, while oncancel is the response to idcancel. The former corresponds to the keyboard's enter, and the latter corresponds to ESC.
Oncancel ()-> destroywindow ()-> ondestroy ()-> onncdestroy ()
Onok ()-> destroywindow ()-> ondestroy ()-> onncdestroy ()
Onncdestroy () finally calls postncdestroy ()
Onok () and oncancel ()
As described above, onok is the response to id_ OK, while oncancel is the response to idcancel. The former corresponds to the keyboard's enter, and the latter corresponds to ESC.
Both onok () and oncancel () call enddialog (). onok calls updatedata (true), but oncacel () does not.
At the end of onok (), when the destroywindow is entered, the window is not actually closed and can still be displayed in showwindow.
The modal dialog box can be destroyed using enddialog, and the non-modal dialog box must be destroyed using destroywindow.
See onok and oncancel http://ponymaggie.blog.sohu.com/135823530.html
Enddialog ()
This function clears a modal dialog box and causes the system to abort any processing of the dialog box.
You can use the default onok () and oncancel () for domodal windows. The base class method calls the enddialog () method.
It can only be used in the message processing function of the dialog box. After this function is called, it does not delete the dialog box immediately, but sets the end mark in the operating system. When the operating system detects this flag, it deletes the message loop in 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 dialogbox function is used to create a dialog box. In this way, the message wm_initdialog is sent before the dialog box is displayed, this gives the dialog box the opportunity to initialize the display of all the above windows or controls, such as setting text box strings. Finally, when you click the OK or cancel button, you will receive two Commands: idok or idcancel. Then you can call the function enddicel to end the life of the dialog box.
See Windows API one-day training (18) enddialog function "http://blog.csdn.net/caimouse/archive/2007/07/30/1716140.aspx
Destroywindows ()
Cwnd: destroywindow: destroys m_hwnd (which must not be empty), destroys its menus, timers, and other cleanup tasks.
: Destroywindow: deactivates the destroyed window, loses the input focus, and sends wm_destroy and wm_ncdestroy messages to the window and Its subwindows. If the destroyed window is a child window and the wm_noparentnotfiy style is not set, the wm_parentnofity message is sent to the parent window.
Cwnd: ondestroy (), cwnd: onncdestroy (), and cwnd: postncdestroy ()
Cwnd: ondestroy () calls the default processing function default ().
Cwnd: onncdestroy () first checks whether the main window of the current thread is the window. If yes and the module is not a DLL, The wm_quit message is sent to end the program;
Then, determine whether the current thread's activity window is the window. If so, set the activity window to null;
Then, clear the tooltip window and call default to process wm_ncdestroy messages and unsubclass by Windows by default.
Window object separation (detach );
Finally, call the virtual function postncdestoy ().
See the MFC tutorial _ application exit http://www.vczx.com/tutorial/mfc/mfc6.php.
To sum up, destroywindows can be understood as active, while ondestroy is passive.
The user calls destroywindows to close the window. When the window is closed, the ondestroy function is called!
If you call destroywindow (), the system will send a message of wm_destroy, which will call the ondestroy () function.
Destroywindow () ------> wm_destroy + wm_ncdestroy ------> ondestroy ()
Message sending Response Message ing
In additionIf you want to prompt the user when exiting, you should handle it in onclose (), onok (), oncancel (), instead of responding to wm_destroy.
Because the window has been destroyed (but the application has not exited ). The system calls
Postquitmessage () delivers wm_quit messages to the Message team and ends the message loop. (See "VC ++ in-depth explanation" P18)
Finally, pay attention to a problem. When we create a non-modal window, we may write
{
Cdialog * pwnd = new cmydialog ();
Pwnd-> Create (......);
Pwnd-> showwindow (sw_show );
}
You can create a window in a module or function, but you cannot know when to close the window. Pwnd is just a local variable. How can we analyze it?
This is usually implemented by reloading the virtual function postncdestroy ().
Void cmydialog: postncdestroy ()
{
Cdialog: postncdestroy ();
Delete this;
}
Why should I put Delete this of the dialog box class in postncdestroy instead of onncdestroy??
This is because onncdestroy is called only by the created window. If a window fails to be created (for example, precreatewindow), no window is sent.
Wm_ncdestroy message. Postncdestroy is completely deleted in the Object window, after onncdestroy, or even after the window fails to be created
Called.
The above is a summary of the knowledge about how to close and destroy the MFC window.