Search code examples
loopswinapimessage

why am i unable to create a successful message loop with PeekMessage()?


My guess it's somehow receiving a WM_QUIT message, because that is what the while loop revolves around ( which according to the proc function happens whenever a WM_DESTROY message is processed)

The window automatically closes whenever i use PeekMessage instead of GetMessage , Im using PeekMessage in order to run the loop at maximum speed

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);

here's the simple WinProc function

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);
}

Solution

  • You are assigning WM_QUIT to msg.message instead of comparing it.