消息和方法说明 摘自:MSDN Library for Visual Studio 2005
WM_CLOSE Notification
The WM_CLOSE message is sent as a signal that a window or an application should terminate.
An application can prompt the user for confirmation, prior to destroying a window, by processing the WM_CLOSE message and calling the DestroyWindow function only if the user confirms the choice.
By default, the DefWindowProc function calls the DestroyWindow function to destroy the window.
WM_DESTROY Notification
The WM_DESTROY message is sent when a window is being destroyed. It is sent to the window procedure of the window being destroyed after the window is removed from the screen.
This message is sent first to the window being destroyed and then to the child windows (if any) as they are destroyed. During the processing of the message, it can be assumed that all child windows still exist.
CWnd::OnNcDestroy
Called by the framework when the nonclient area is being destroyed, and is the last member function called when the Windows window is destroyed.
The default implementation performs some cleanup, then calls the virtual member function PostNcDestroy.
Override PostNcDestroy if you want to perform your own cleanup, such as a delete this operation. If you override OnNcDestroy, you must call OnNcDestroy in your base class to ensure that any memory internally allocated for the window is freed.
Destroying Frame Windows(Window Destruction Sequence )
The MFC framework manages window destruction as well as creation for those windows associated with framework documents and views. If you create additional windows, you are responsible for destroying them.
In the framework, when the user closes the frame window, the window's default OnClose handler calls DestroyWindow. The last member function called when the Windows window is destroyed is OnNcDestroy, which does some cleanup, calls the Default member function to perform Windows cleanup, and lastly calls the virtual member function PostNcDestroy. The CFrameWnd implementation of PostNcDestroy deletes the C++ window object.You should never use the C++ delete operator on a frame window. Use DestroyWindow instead.
When the main window closes, the application closes. If there are modified unsaved documents, the framework displays a message box to ask if the documents should be saved and ensures that the appropriate documents are saved if necessary.
总结:
关闭过程如下:
用户点击主窗口的关闭按钮
发送WM_CLOSE消息(消息响应默认执行DestroyWindow)
发送WM_DESTROY
发送WM_NCDESTROY(调用 PostNcDestroy)