peekmessage,getmessage区别浅入浅出

来源:互联网 发布:为什么不读博 知乎 编辑:程序博客网 时间:2024/05/16 18:04
区别一:
peekmessage不管消息队列里有没有消息都会马上返回,有消息返回消息,没消息返回空值,
getmessage等待到有消息的时候才返回,
区别二:
peekmessage可以根据参数决定是否将消息保留在队列中,
PM_NOREMOVE:该参数指示保留消息
PM_REMOVE:该参数指示移去消息
而getmessage获得消息后回把消息从消息队列中删去

例程:
// GetMessage Damo
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR szCmdLine,
                                 int iCmdShow)
{
        MSG   msg ;

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

// PeekMessage() Damo
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR szCmdLine,
                                 int iCmdShow)
{
        MSG   msg ;

        while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage (&msg) ;
            DispatchMessage (&msg) ;
        }
        return TRUE ;
}
原创粉丝点击