如果是下面的hwnd,则当窗口关闭后,hwnd无效了,无法捕获WM_QUIT消息了。
while(GetMessage(&msg,hwnd,0,0))
{TranslateMessage(&msg);
DispatchMessage(&msg);
}
hWnd
| Value | Meaning |
|---|---|
| NULL | GetMessage retrieves messages for any window that belongs to the calling thread and thread messages posted to the calling thread using thePostThreadMessage function. |
参数hwnd设置为NULL,具有特殊的含义,GetMessage 会获取线程中的所有窗口的消息,以及通过PostThreadMessage投递的消息。
After you receive the message WM_DESTROY and the window is destroyed. but your process will not quit until you get a WM_QUIT message, if you use hwnd to GetMessage,that means you only receive msg that belong that window,but the window has destroyed,so you can not receive the WM_QUIT message.
当接受到WM_DESTROY消息后,销毁窗口,但是进程不会退出,直到收到了WM_QUIT消息。如果GetMessage的参数是hwnd,那么只接受属于这个窗口的消息,当窗口销毁后,不能接受WM_QUIT消息,程序永远不会退出。
正确的用法是
while(GetMessage(&msg,NULL,0,0))
{TranslateMessage(&msg);
DispatchMessage(&msg);
}
本文详细解释了在窗口消息循环中使用GetMessage函数的注意事项,特别是当窗口被销毁后如何确保接收WM_QUIT消息以使进程正常退出。通过改变hwnd参数的设置,避免了仅接收到特定窗口的消息而忽略系统级消息的问题。
368

被折叠的 条评论
为什么被折叠?



