如何截取消息:“WM_QUIT || WM_DESTROY || WM_CLOSE“ WinAPI [英] How to intercept a message: "WM_QUIT || WM_DESTROY || WM_CLOSE" WinAPI

查看:227
本文介绍了如何截取消息:“WM_QUIT || WM_DESTROY || WM_CLOSE“ WinAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将此代码用于主循环(我的函数):

I use this code for the main loop (my function):

    while (running)
{
    if(is_close)
    {
        Log().push_log("close", "message close!", logInfo);
        running = active = false;

        break;
    }

    while (PeekMessage(&msg, g_hWnd, 0, 0, PM_REMOVE))
    {
        std::cout << "Wnd: " << msg.message << std::endl;

        if (msg.message == WM_QUIT || msg.message == WM_DESTROY || msg.message == WM_CLOSE)
        {
            MessageBox(0, "Hello, World", "Hello", MB_OK);
            running = false;
        }
        // TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    if (running && active)
        render.DrawObject(g_hDC);
}

那么,我使用WndProc:

Well, then I use WndProc:

LRESULT CALLBACK GLWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    std::cout << "Wnd Proc: " << msg << std::endl;

    return DefWindowProc(hWnd, msg, wParam, lParam);
}



当我尝试获取消息 WM_QUIT在我的函数中, WM_DESTROY WM_CLOSE

我如何获得此消息?

推荐答案

PeekMessage或GetMessage只会返回使用PostMessage()发布到消息队列的消息。这永远不会是WM_CLOSE或WM_DESTROY,这些消息与SendMessage()一起发送,直接传递到窗口过程,不进入消息队列。你不会得到WM_QUIT,除非你有一个PostQuitMessage()调用你的代码,你不会。

PeekMessage or GetMessage will only return messages that were posted to the message queue with PostMessage(). That will never be WM_CLOSE or WM_DESTROY, those messages are sent with SendMessage(), directly delivered to the window procedure and don't go in the message queue. You won't get WM_QUIT unless you've got a PostQuitMessage() call in your code, you don't.

你真的必须写一个窗口过程你的主窗口。只需处理WM_DESTROY和调用PostQuitMessage(0)就足够了。

You really do have to write a window procedure for your main window. Simply handling WM_DESTROY and calling PostQuitMessage(0) should be enough.

LRESULT CALLBACK GLWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    if (msg == WM_DESTROY) PostQuitMessage(0);
    return DefWindowProc(hWnd, msg, wParam, lParam);
}

您现在可以在游戏循环中获得WM_QUIT。

You'll now get WM_QUIT in your game loop.

这篇关于如何截取消息:“WM_QUIT || WM_DESTROY || WM_CLOSE“ WinAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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