The DestroyWindow() documentation says the following:
The function also destroys the window's menu, flushes the thread message queue,
Does "flushes the thread message queue" means that it will remove the messages from the message queue for the window that I want to destroy only?
答案 0 :(得分:4)
尽管文档在此方面并不明确,但它确实按照您的建议行事。发布到已销毁窗口的消息将被刷新,而其他窗口(或发布到该线程)的消息将保留在线程的队列中。
以下示例程序演示了这一点 - 如果您的建议属实,则不应触发断言(在我的测试中,它们不会触发)。
正如@HarryJohnston在评论中指出的那样,通过一个线程销毁一个窗口并不会破坏线程所拥有的任何其他窗口(除了子窗口和被破坏窗口的拥有窗口),所以如果他们的话可能会有很多问题已发布的消息已被删除,实际上无缘无故。
#include <windows.h>
#include <assert.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
assert(message != WM_APP + 1);
return DefWindowProc(hWnd, message, wParam, lParam);
}
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEXW wcex{};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.lpszClassName = L"msgflushtest";
assert(RegisterClassExW(&wcex));
HWND hWnd = CreateWindowEx(0, L"msgflushtest", nullptr, WS_POPUP, 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
HWND_DESKTOP, nullptr, hInstance, nullptr);
assert(hWnd);
assert(PostMessage(hWnd, WM_APP + 1, 0, 0)); // should be flushed
assert(PostThreadMessage(GetCurrentThreadId(), WM_APP + 2, 0, 0)); // should not be flushed
DestroyWindow(hWnd);
MSG msg;
assert(!PeekMessage(&msg, nullptr, WM_APP + 1, WM_APP + 1, PM_REMOVE));
assert(PeekMessage(&msg, nullptr, WM_APP + 2, WM_APP + 2, PM_REMOVE));
return 0;
}