gpt4 book ai didi

c - Win32 消息循环 : Quitting after window closes with GetMessage(&msg, NULL、0、0)?

转载 作者:太空宇宙 更新时间:2023-11-04 02:17:46 26 4
gpt4 key购买 nike

如果我有下面的代码,我如何检测窗口何时关闭,以便我可以退出? r 似乎永远不会得到值 -1 0,我需要为整个线程处理消息,不仅仅是当前窗口。

HWND hWnd = CreateWindowExW(0, L"Edit", L"My Window", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 200, NULL, NULL, NULL, NULL);
ShowWindow(hWnd, SW_SHOWDEFAULT);
MSG msg;
BOOL r;
while ((r = GetMessageW(&msg, NULL, 0, 0)) != 0)
{
if (r == -1) { break; }
TranslateMessage(&msg);
DispatchMessageW(&msg);
}

最佳答案

等待 r = -1 并不是检测窗口已关闭的方式。返回值 -1 不是正常情况:它表示消息循环中发生了错误。

来自documentation :

Return Value

Type: BOOL

If the function retrieves a message other than WM_QUIT, the return value is nonzero.

If the function retrieves the WM_QUIT message, the return value is zero.

If there is an error, the return value is -1. For example, the function fails if hWnd is an invalid window handle or lpMsg is an invalid pointer. To get extended error information, call GetLastError.

GetMessage 从队列中检索到 WM_QUIT 消息时,它将返回值 0,您应该结束循环。

如果您只想知道窗口何时关闭,您可能想要处理 WM_CLOSEWM_DESTROY消息。有关这些消息的讨论,请参阅此问题的答案:What is the difference between WM_QUIT, WM_CLOSE, and WM_DESTROY in a windows program?

关于c - Win32 消息循环 : Quitting after window closes with GetMessage(&msg, NULL、0、0)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4893295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com