gpt4 book ai didi

c++ - GetMessage/PeekMessage - 删除消息队列中的所有消息

转载 作者:太空宇宙 更新时间:2023-11-04 13:03:44 26 4
gpt4 key购买 nike

我有以下代码

SendApp,点击按钮[X]时,执行以下代码

HWND pHWndReceiveApp = FindWindowA(NULL, "ReceiveApp");
if (NULL == pHWndReceiveApp)
{
MessageBox(0, MSG_FAIL, CAPTION, 0);
return;
}
for (int i = 0; i < 7; i++)
{
PostMessageA(pHWndReceiveApp, 9, 9, 0);
}
MessageBox(0, MSG_OK, CAPTION, 0);

ReceiveApp,这只是一个接收SendApp消息的应用

int i = 0;
msg.message = 69;
while (WM_QUIT != msg.message)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
{
if (msg.message == 9)
{
//I want to remove all messages in the message queue before increase i
++i;
Sleep(1000);
}

TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

如您所见,我想在 ReceiveApp 中增加变量 i 之前删除消息队列中的所有消息。我读了this article看看那个

PM_REMOVE - Messages are removed from the queue after processing by PeekMessage.

我认为所有消息都应该被删除,但它们没有,在我按下 SendApp 中的按钮 [X] 后,ReceiveApp 中的变量 i 仍然增加了 7。

那么如何删除消息队列中的所有消息呢?

最佳答案

我明白了。我们可以在 while 循环中使用 PeekMessage 来清除队列中的消息,如下所示

#define ID 9
//...
int i = 0;
msg.message = 69;

while (GetMessage(&msg, 0, 0, 0))
{
if (msg.message == ID)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
while (PeekMessage(&msg, NULL, ID, ID, PM_REMOVE) > 0) //Clear message queue!
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//I want to remove all message in the message queue before increase i
++i;
Sleep(1000);
}
}

关于c++ - GetMessage/PeekMessage - 删除消息队列中的所有消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43222685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com