窗口銷毀的相關(guān)函數(shù)與消息
DestroyWindow函數(shù)
DestroyWindow銷毀窗口的正牌函數(shù)。
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的處理過程總結(jié)要點如下:
1. 向父窗口發(fā)送WM_PARENTNOTIFY消息(僅當(dāng)窗口具有WS_EX_NOPARENTNOTIFY樣式);
2. 向窗口發(fā)送WM_DESTROY消息;
3. 使窗口轉(zhuǎn)入非活動狀態(tài),移除鍵盤焦點;
4. 銷毀子窗口(應(yīng)該是通過遞歸調(diào)用DestroyWindow完成);
5. 銷毀目標(biāo)窗口資源:銷毀菜單,清空線程的消息隊列,銷毀與窗口過程相關(guān)的定時器,解除窗口對剪貼板的擁有權(quán),打斷剪貼板器的查看鏈;
6. 向窗口發(fā)送WM_NCDESTROY消息;
上述條目大致從前往后執(zhí)行(1、2、3的順序不一定,5、6的順序不一定)。依據(jù)是:
1. 根據(jù)WM_PARENTNOTIFY說明:銷毀窗口時,系統(tǒng)在任何銷毀窗口的操作執(zhí)行前發(fā)送WM_PARENTNOTIFY至父窗口;
2. 根據(jù)WM_DESTROY說明:此消息先被發(fā)送至目標(biāo)窗口,然后發(fā)送給目標(biāo)窗口的所有子窗口,處理此消息時,可以假定所有子窗口還存在著;
3. 根據(jù)DestroyWindow說明:函數(shù)先銷毀子窗口及所擁有的窗口,然后銷毀目標(biāo)窗口;
4. 根據(jù)WM_NCDESTROY說明:與WM_DESTROY相反的是,此消息在所有子窗口銷毀之后發(fā)送; 沒有去看匯編代碼,只是根據(jù)各個MSDN說明來大致推測的,基本夠用了吧。
根據(jù)MSDN說明,不能在非創(chuàng)建線程上調(diào)用DestroyWindow;若要在其它線程上關(guān)閉窗口,不可以直接調(diào)用DestroyWindow(錯誤"拒絕訪問"),也不可以只發(fā)送WM_DESTROY,因為DestroyWindow才能完整的關(guān)閉窗口,WM_DESTROY消息只是關(guān)閉流程的一部分,甚至是最"無用"的一部分--WM_DESTROY只是用戶響應(yīng)的接口,而真正清理窗口的工作由DestroyWindow的其它部分完成。
要在其它線程上關(guān)閉窗口,可以向窗口發(fā)送WM_CLOSE消息,若窗口過程未在WM_CLOSE的處理中取消關(guān)閉操作,則DefWindowProc會默認調(diào)用DestroyWindow(彼時自然是在窗口的創(chuàng)建線程上)。
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消息提供取消關(guān)閉窗口操作的接口,也可以作為窗口關(guān)閉的關(guān)聯(lián)處理的統(tǒng)一入口。點擊窗口右上角的關(guān)閉按鈕將導(dǎo)致窗口收到WM_CLOSE消息,一般情況下,菜單中的Exit項的處理代碼中也應(yīng)向窗口發(fā)送WM_CLOSE消息,這樣可以使無論何種方式觸發(fā)的窗口關(guān)閉操作,其程序流均會流入WM_CLOSE消息的處理過程。在WM_CLOSE中可以對程序狀態(tài)進行確認,自動或由用戶確認是否取消關(guān)閉操作。
與接下來的其它消息相比,WM_CLOSE的特殊之處在于它發(fā)生在銷毀過程啟動之前。用戶處理完WM_CLOSE消息后,如未取消,則DefWindowProc將調(diào)用DestroyWindow來關(guān)閉窗口,一旦進入DestroyWindow,則關(guān)閉過程將不可逆轉(zhuǎn)。
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.
關(guān)于WM_DESTROY與WM_NCDESTROY,從各種說明可以看出,一個大約充當(dāng)關(guān)閉操作啟動時的通知接口,一個大約充當(dāng)關(guān)閉操作完成時的通知接口。這也是我認為在DestroyWindow的處理流程中,第6條在第5條之后的原因。
另外,說明中有個值得一提的地方在于:WM_DESTROY用于釋放關(guān)聯(lián)到(associated to)窗口的已分配內(nèi)存對象,WM_NCDESTROY用于釋放內(nèi)部為窗口分配的(internally allocated for)內(nèi)存。 可以想象,后者應(yīng)當(dāng)指窗口的內(nèi)核數(shù)據(jù),但前者則有各種可能,菜單資源?GDI資源?異或是子窗口資源?這個就不好說了。 而且,根據(jù)這兩句話的表述,似乎這兩個清理操作是在消息的處理過程中發(fā)生的,然而顯然又不是由用戶來完成的工作,那么就應(yīng)當(dāng)是在DefWindowProc中完成的了。為什么不直接拿到Destroywindow中來執(zhí)行呢?
ActiveX控件的銷毀
銷毀控件的最好方法是銷毀宿主窗口。可以通過調(diào)用 CWindow::DestroyWindow 或 DestroyWindow API 顯式地進行銷毀,或者讓宿主窗口的父級窗口的毀壞導(dǎo)致其死亡,從而隱式地進行銷毀。這些方法的任何一種都將安全地銷毀寄宿的控件。請注意,CAxWindow 對象的毀壞"不會"導(dǎo)致基礎(chǔ)"AtlAxWin7"窗口的毀壞。
來自:http://technet.microsoft.com/zh-cn/query/cc468138