PeekMessage() 与 GetMessage() 区别

本文详细介绍了Windows编程中PeekMessage和GetMessage两个消息处理函数的使用方法及参数说明。通过示例代码展示了如何利用这两个函数进行消息循环处理,包括如何响应退出消息和其他消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

functionParametersReturn Values
PeekMessageLPMSG lpMsg,
pointer to structure for message

HWND hWnd
handle to window

UINT wMsgFilterMin
first message

UINT wMsgFilterMax
last message

UINT wRemoveMsg
removal flags
BOOL

Remarks 
If a message is available, the return value is nonzero.
If no messages are available, the return value is zero.


functionParametersReturn Values
GetMessageLPMSG lpMsg
address of structure with message

HWND hWnd
handle of window

UINT wMsgFilterMin
first message

UINT wMsgFilterMax
last message
BOOL

Remarks 
If the function retrieves a message other than WM_QUIT, the return value is nonzero.
If the function retrieves the WM_QUIT message, the return value is zero.
If there is an error, the return value is -1. For example, the function fails if hWnd is an invalid window handle or lpMsg is an invalid pointer. To get extended error information, callGetLastError.


Example

PeekMessage

 while (!bQuit)
    {
        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage (&msg);
                DispatchMessage (&msg);
            }
        }

GetMessage

while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

转载于:https://www.cnblogs.com/laohaozi/p/8266610.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值