我有以下代码
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 - 处理后,消息将从队列中删除 的PeekMessage。
我认为应该删除所有邮件,但是他们没有,在SendApp中按下按钮[X]后,ReceiveApp中的变量i仍然增加了7。
那么如何删除邮件队列中的所有邮件?
答案 0 :(得分:0)
我明白了。我们可以在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);
}
}