我对消息循环在 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/