为什么我无法使用PeekMessage()创建成功的消息循环?

时间:2016-04-21 09:30:17

标签: loops winapi message

我的猜测是以某种方式接收WM_QUIT消息,因为这就是while循环所围绕的(根据proc函数在处理WM_DESTROY消息时发生)

每当我使用PeekMessage而不是GetMessage时,窗口会自动关闭,我使用PeekMessage以最大速度运行循环

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
if(!CreateMainWindow(hinstance, nCmdShow))
   return false;
//this works
while (GetMessage(&msg, (HWND) NULL, 0 , 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
return (int) msg.wParam;
    UNREFERENCED_PARAMETER(lpCmdLine);
}    

//this automatically closes the window
int done = 0;
while (!done)
{
    if (PeekMessage (&msg, NULL, 0 ,0, PM_REMOVE))
    {

        if (msg.message = WM_QUIT)
            done = 1;
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}
return msg.wParam;
    UNREFERENCED_PARAMETER(lpCmdLine);

这里是简单的WinProc功能

LRESULT CALLBACK WinProc ( HWND hWnd, UINT msg, WPARAM wParam, LPARAM   
lParam)
{
switch( msg)
{
      Case WM_DESTROY: 
      PostQuitMessage(0);
      return 0;
}
return DefWindowProc ( hWnd, msg, wParam, lParam);
}

1 个答案:

答案 0 :(得分:7)

您正在将WM_QUIT分配给msg.message而不是比较它。