GetMessage () and PeekMessage () and SendMessage () and PostMessage ()

来源:互联网 发布:公司可以注册gov域名 编辑:程序博客网 时间:2024/05/16 05:25
  
 
GetMessage () and PeekMessage ()
GetMessage 其唯一用途就是从事件对列中获得消息,并进行处理。当程序在等待通过 GetMessage 传递的消息时,主事件循坏基本上是锁定的。
那,如果我们要处理实时的事件循坏该怎么办呢?Windows 为我们提供了另一个函数 PeekMessage,他们的原型几乎是一样的,唯一的不同是 PeekMessage 比 GetMessage 多了一个参数:UINT wRemoveMsg // removal flags。
对于wRemoveMsg ,有效的标志有
PM_NOREMOVE,PeekMessage 处理之后,消息没有从序列中去除
PM_REMOVE,PeekMessage 处理之后,消息已经从序列中去除
怎样来实现实时事件循坏呢?
通过利用PeekMessage 来判断消息序列中是否有消息,如果有,就处理它;否则继续处理其他的游戏逻辑并重复进行。
他们的函数原型如下:
BOOL GetMessage (
 LPMSG lpMsg, // pointer to structure for message
 HWND hWnd, // handle to window
 UINT wMsgFilterMin, // first message
 UINT wMsgFilterMax ) // last message
 
BOOL PeekMessage (
 LPMSG lpMsg, // pointer to structure for message
 HWND hWnd, // handle to window
 UINT wMsgFilterMin, // first message
 UINT wMsgFilterMax, // last message
 UINT wRemoveMsg ); // removal flags
 
SendMessage () and PostMessage ()
SendMessage 向窗口传递一个要求立即处理的消息。接收窗口处理完该消息后,该函数便紧接着 WinProc 返回。是同步的。
PostMessage 将消息发往窗口的消息序列,而后直接返回。如果不在意在消息被处理以前的时间延迟,或者该消息的优先级较低,就可以使用该函数。是异步的。
这两个函数的原型几乎一样,唯一的不同是返回值不同。SendMessage 返回 LRESULT,PostMessage 返回的是 BOOL。他们的函数原型如下:
LRESULT SendMessage ( HWND hWnd, // handle of destination windonw
 UINT Msg, // message to send
 WPARAM wParam, // first message parameter
 LPARAM lParam); // second message parameter
 
BOOL PostMessage ( HWND hWnd, // handle of destination windonw
 UINT Msg, // message to send
 WPARAM wParam, // first message parameter
 LPARAM lParam); // second message parameter
 
 
原创粉丝点击