无限窗口消息循环 [英] Infinite windows message loop

查看:155
本文介绍了无限窗口消息循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程式中有这个讯息循环:

I have this message loop in my program:

while (true) {
    if (PeekMessage(&msg, window, 0, 0, PM_REMOVE)) {
        if (msg.message == WM_QUIT) {
            MessageBox(NULL, L"Quit", L"", 0);
            break;
        }
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    } else {
        Render();
    }
}

此循环永远不会结束。即使主窗口消失,它也不会显示消息框。
这是WndProc代码:

This loop never ends. It never displays the message box even though main window disappears. Here is the WndProc code:

switch (msg) {

    case WM_CLOSE :
        DestroyWindow(hwnd);
        break;

    case WM_DESTROY :
        PostQuitMessage(0);
        break;

    default :
        return DefWindowProc(hwnd, msg, wParam, lParam);
        break;
}

return 0;

有人可以帮助我吗?

推荐答案

正在调用 PeekMessage(& msg,window ,...)。如果 window 不是 NULL ,你永远不会得到 WM_QUIT WM_QUIT 与窗口没有关联。

You're calling PeekMessage(&msg, window, ...). If window isn't NULL, you'll never get WM_QUIT, because WM_QUIT is not associated with a window.

而是调用 PeekMessage / GetMessage NULL HWND DispatchMessage 将根据需要将其发送到右侧 WndProc 。 (一般来说,制作 GetMessage / PeekMessage 过滤器 HWND 是一个坏主意

Instead, just call PeekMessage/GetMessage with a NULL HWND. DispatchMessage will send it to the right WndProc as necessary. (In general, making GetMessage/PeekMessage filter by HWND is a bad idea.)

这篇关于无限窗口消息循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆