c++ - win32 应用程序是否只有一个消息循环?还是每个窗口一个消息循环?

标签 c++ c winapi

我对消息循环在 win32 编程中的工作方式有点困惑。在我的 WinMain 中,我总是放置以下内容:

while ( GetMessage ( &msg, NULL, 0, 0 ) > 0 )
{
    TranslateMessage ( &msg );
    DispatchMessage ( &msg );
}

这是一个 while 循环,它几乎一直运行到您的应用程序停止为止。这是否意味着每个应用程序而不是每个窗口都有一个消息循环?

最佳答案

来自 About Messages and Message Queues :

Applications with multiple threads can include a message loop in each thread that creates a window.

注意一个消息队列可以支持多窗口... GetMessage的第二个参数是您要查看其消息的窗口的句柄。如果 NULL 则线程的所有窗口。

作为第二个说明,可以在没有窗口的情况下创建消息队列(至少从 Windows 2000 开始)。 PostThreadMessage 的文档中对此进行了描述:

In the thread to which the message will be posted, call PeekMessage as shown here to force the system to create the message queue.

PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE)

关于c++ - win32 应用程序是否只有一个消息循环?还是每个窗口一个消息循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18111482/

相关文章:

c++ - 在头文件中使用模板

c++ - 以下片段的区别

C编程: array not have any bound

c++ - 解决 SPOJ DIEHARD 的正确方法是什么?

c++ - 使用 std::is_sorted 测试数组,但具有预设精度

c++ - 在 ListView 中禁用水平滚动条

perl - 如何在 Win32 上使用 Perl 重新映射键盘?

winapi - 在 Windows 中查找未记录的 API

c++ - 用 glfw 隐藏鼠标光标

c - putchar() 没有返回值?