【销毁窗口】关键消息和方法

消息和方法说明 摘自: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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值