窗口銷毀的相關函數與消息
DestroyWindow函數
DestroyWindow銷毀窗口的正牌函數。
DestroyWindows的MSDN說明如下:
The DestroyWindow function destroys the specified window. The function sends WM_DESTROY and WM_NCDESTROY messages to the window to deactivate it and remove the keyboard focus from it. The function also destroys the window's menu, flushes the thread message queue, destroys timers, removes clipboard ownership, and breaks the clipboard viewer chain (if the window is at the top of the viewer chain).
If the specified window is a parent or owner window, DestroyWindow automatically destroys the associated child or owned windows when it destroys the parent or owner window. The function first destroys child or owned windows, and then it destroys the parent or owner window.
DestroyWindow also destroys modeless dialog boxes created by the CreateDialog function.
A thread cannot use DestroyWindow to destroy a window created by a different thread.
If the window being destroyed is a child window that does not have the WS_EX_NOPARENTNOTIFY style, a WM_PARENTNOTIFY message is sent to the parent.
DestroyWindow的處理過程總結要點如下:
1. 向父窗口發送WM_PARENTNOTIFY消息(僅當窗口具有WS_EX_NOPARENTNOTIFY樣式);
2. 向窗口發送WM_DESTROY消息;
3. 使窗口轉入非活動狀態,移除鍵盤焦點;
4. 銷毀子窗口(應該是通過遞歸調用DestroyWindow完成);
5. 銷毀目標窗口資源:銷毀菜單,清空線程的消息隊列,銷毀與窗口過程相關的定時器,解除窗口對剪貼板的擁有權,打斷剪貼板器的查看鏈;
6. 向窗口發送WM_NCDESTROY消息;
上述條目大致從前往后執行(1、2、3的順序不一定,5、6的順序不一定)。依據是:
1. 根據WM_PARENTNOTIFY說明:銷毀窗口時,系統在任何銷毀窗口的操作執行前發送WM_PARENTNOTIFY至父窗口;
2. 根據WM_DESTROY說明:此消息先被發送至目標窗口,然后發送給目標窗口的所有子窗口,處理此消息時,可以假定所有子窗口還存在著;
3. 根據DestroyWindow說明:函數先銷毀子窗口及所擁有的窗口,然后銷毀目標窗口;
4. 根據WM_NCDESTROY說明:與WM_DESTROY相反的是,此消息在所有子窗口銷毀之后發送; 沒有去看匯編代碼,只是根據各個MSDN說明來大致推測的,基本夠用了吧。
根據MSDN說明,不能在非創建線程上調用DestroyWindow;若要在其它線程上關閉窗口,不可以直接調用DestroyWindow(錯誤"拒絕訪問"),也不可以只發送WM_DESTROY,因為DestroyWindow才能完整的關閉窗口,WM_DESTROY消息只是關閉流程的一部分,甚至是最"無用"的一部分--WM_DESTROY只是用戶響應的接口,而真正清理窗口的工作由DestroyWindow的其它部分完成。
要在其它線程上關閉窗口,可以向窗口發送WM_CLOSE消息,若窗口過程未在WM_CLOSE的處理中取消關閉操作,則DefWindowProc會默認調用DestroyWindow(彼時自然是在窗口的創建線程上)。
WM_CLOSE消息
WM_CLOSE的MSDN說明如下:
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_CLOSE消息提供取消關閉窗口操作的接口,也可以作為窗口關閉的關聯處理的統一入口。點擊窗口右上角的關閉按鈕將導致窗口收到WM_CLOSE消息,一般情況下,菜單中的Exit項的處理代碼中也應向窗口發送WM_CLOSE消息,這樣可以使無論何種方式觸發的窗口關閉操作,其程序流均會流入WM_CLOSE消息的處理過程。在WM_CLOSE中可以對程序狀態進行確認,自動或由用戶確認是否取消關閉操作。
與接下來的其它消息相比,WM_CLOSE的特殊之處在于它發生在銷毀過程啟動之前。用戶處理完WM_CLOSE消息后,如未取消,則DefWindowProc將調用DestroyWindow來關閉窗口,一旦進入DestroyWindow,則關閉過程將不可逆轉。
WM_DESTROY消息
WM_DESTROY的MSDN說明如下:
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.
A window receives this message through its WindowProc function.
If the window being destroyed is part of the clipboard viewer chain (set by calling the SetClipboardViewer function), the window must remove itself from the chain by processing the ChangeClipboardChain function before returning from the WM_DESTROY message.
最后一句值得注意,不過對剪貼板涉獵不多,暫且不究。
WM_PARENTNOTIFY消息
WM_PARENTNOTIFY的MSDN說明如下:
The WM_PARENTNOTIFY message is sent to the parent of a child window when the child window is created or destroyed, or when the user clicks a mouse button while the cursor is over the child window. When the child window is being created, the system sends WM_PARENTNOTIFY just before the CreateWindow or CreateWindowEx function that creates the window returns. When the child window is being destroyed, the system sends the message before any processing to destroy the window takes place.
A window receives this message through its WindowProc function.
WM_NCDESTROY消息
WM_NCDESTROY的MSDN說明如下:
The WM_NCDESTROY message informs a window that its nonclient area is being destroyed. The DestroyWindow function sends the WM_NCDESTROY message to the window following the WM_DESTROY message. WM_DESTROY is used to free the allocated memory object associated with the window. The WM_NCDESTROY message is sent after the child windows have been destroyed. In contrast, WM_DESTROY is sent before the child windows are destroyed. A window receives this message through its WindowProc function. This message frees any memory internally allocated for the window.
關于WM_DESTROY與WM_NCDESTROY,從各種說明可以看出,一個大約充當關閉操作啟動時的通知接口,一個大約充當關閉操作完成時的通知接口。這也是我認為在DestroyWindow的處理流程中,第6條在第5條之后的原因。
另外,說明中有個值得一提的地方在于:WM_DESTROY用于釋放關聯到(associated to)窗口的已分配內存對象,WM_NCDESTROY用于釋放內部為窗口分配的(internally allocated for)內存。 可以想象,后者應當指窗口的內核數據,但前者則有各種可能,菜單資源?GDI資源?異或是子窗口資源?這個就不好說了。 而且,根據這兩句話的表述,似乎這兩個清理操作是在消息的處理過程中發生的,然而顯然又不是由用戶來完成的工作,那么就應當是在DefWindowProc中完成的了。為什么不直接拿到Destroywindow中來執行呢?
ActiveX控件的銷毀
銷毀控件的最好方法是銷毀宿主窗口。可以通過調用 CWindow::DestroyWindow 或 DestroyWindow API 顯式地進行銷毀,或者讓宿主窗口的父級窗口的毀壞導致其死亡,從而隱式地進行銷毀。這些方法的任何一種都將安全地銷毀寄宿的控件。請注意,CAxWindow 對象的毀壞"不會"導致基礎"AtlAxWin7"窗口的毀壞。
來自:http://technet.microsoft.com/zh-cn/query/cc468138