Win32 消息循环:使用 GetMessage(&msg, NULL, 0, 0) 关闭窗口后退出?

发布于 2024-10-15 12:08:21 字数 500 浏览 2 评论 0原文

如果我有下面的代码,如何检测窗口何时关闭,以便我可以退出? 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);
}

If I have the following code below, how do I detect when the window has been closed, so I can quit? r never seems to get the value -1 0, and I need to process messages for the entire thread, not just the current window.

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随梦而飞# 2024-10-22 12:08:21

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

来自文档

返回值

类型:BOOL

如果函数检索到 WM_QUIT 以外的消息,则返回值非零。

如果函数检索到 WM_QUIT 消息,则返回值为零。

如果出现错误,则返回值为-1。例如,如果 hWnd 是无效的窗口句柄或 lpMsg 是无效的指针,则该函数将失败。要获取扩展错误信息,请调用 GetLastError

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

如果您只想知道窗口何时关闭,您可能需要处理 WM_CLOSEWM_DESTROY 消息。有关这些消息的讨论,请参阅此问题的答案:Windows 程序中的 WM_QUIT、WM_CLOSE 和 WM_DESTROY 有什么区别?

Waiting for r = -1 is not the way you detect that your window has closed. A return value of -1 is not a normal condition: it indicates that an error has occurred in the message loop.

From the 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.

When GetMessage retrieves a WM_QUIT message from the queue, it will return a value of 0, and you should end the loop.

If you just want to know when the window has closed, you probably want to handle either the WM_CLOSE or WM_DESTROY messages. For a discussion of these messages, see the answers to this question: What is the difference between WM_QUIT, WM_CLOSE, and WM_DESTROY in a windows program?

拔了角的鹿 2024-10-22 12:08:21

我找到了一个解决方案: WM_NULL
消息循环可以独立于WndProc自行处理问题:

// written in C# 
MSG msg = new MSG();
while (GetMessage(out msg, window, 0, 0))
{
    if ((msg.message == WM_NULL) && !IsWindow(window))
        break;
    TranslateMessage(ref msg);
    DispatchMessage(ref msg);
}
Console.WriteLine("yeah, out of loop ^^");

根据我的观察:当窗口被销毁时GetMessage检索WM_NULL< /code> 消息没有暂停(第一个提示)和 IsWindow可以检查窗口(确认)。

I found a solution for this: WM_NULL.
The message loop can handle the matter on its own independently of WndProc:

// written in C# 
MSG msg = new MSG();
while (GetMessage(out msg, window, 0, 0))
{
    if ((msg.message == WM_NULL) && !IsWindow(window))
        break;
    TranslateMessage(ref msg);
    DispatchMessage(ref msg);
}
Console.WriteLine("yeah, out of loop ^^");

From my observation: When window is destroyed GetMessage retrieves WM_NULL messages without pause (1st hint) and IsWindow can check the window (affirmation).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文