Windows线程消息队列

来源:互联网 发布:网络用语lg是什么意思 编辑:程序博客网 时间:2024/06/06 09:29

每一个线程都对应有一个消息队列。

消息分为两类:
1.窗口消息(window message)
2.线程消息即非窗口消息(thread message)

在线程中常采用如下的方式来获取消息及维持线程的运行:

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

::GetMessage(&msg, NULL, 0, 0)保证获取所有的窗口消息及线程消息,
::DispatchMessage(&msg)用于将窗口消息发送给对应的窗口过程函数处理,
对应获取得到的线程消息,DispatchMessage是不做任何处理的,
所以,对于线程消息,应这样写代码

while (::GetMessage(&msg, NULL, 0, 0)){    if (msg.hwnd == NULL)    {        // 处理线程消息        ...        continue;    }    ::TranslateMessage(&msg);    ::DispatchMessage(&msg);}
0 0