Windows endless message loop

I have this message loop in my program:

while (true) { if (PeekMessage(&msg, window, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) { MessageBox(NULL, L"Quit", L"", 0); break; } TranslateMessage(&msg); DispatchMessage(&msg); } else { Render(); } } 

This cycle never ends. It never displays a message box, even if the main window disappears. Here is the WndProc code:

 switch (msg) { case WM_CLOSE : DestroyWindow(hwnd); break; case WM_DESTROY : PostQuitMessage(0); break; default : return DefWindowProc(hwnd, msg, wParam, lParam); break; } return 0; 

Can someone please help me? I literally stretch my hair.

0
source share
1 answer

You call PeekMessage(&msg, window, ...) . If window not NULL , you will never get WM_QUIT because WM_QUIT not associated with the window.

Instead, just call PeekMessage / GetMessage with a NULL HWND . DispatchMessage forward it to WndProc as needed. (In general, creating a GetMessage / PeekMessage HWND filter is a bad idea. )

+9
source

Source: https://habr.com/ru/post/916943/


All Articles