Win32 application doesn't close on window exits

thebluetropics 1,046 Reputation points
2022-09-17T04:32:04.77+00:00

I have simple program:

   MSG msg = { };  
     
           while (true) {  
               while (PeekMessageW(&msg, state.hwnd, 0, 0, PM_REMOVE)) {  
                   TranslateMessage(&msg);  
                   DispatchMessageW(&msg);  
               }  
     
               if (msg.message == WM_QUIT) return;  
           }  

I am using VS2022, and when I run the debugger, the app does work, the window showing, but when I close the window, the debugger still running.
I am new to PeekMessage, and this is the first tutorial I tried out.

The full code is here

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,588 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,692 questions
{count} votes

Accepted answer
  1. Castorix31 84,546 Reputation points
    2022-09-17T05:58:21.207+00:00

    You can do :

            while (TRUE)  
            {  
                if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))  
                {  
                    if (msg.message == WM_QUIT)  
                    {  
                        break;  
                    }  
                    TranslateMessage(&msg);  
                    DispatchMessage(&msg);  
                }  
                OutputDebugStringW(L"Test\n");  
            }  
    

    (and add in WindowProcedure :

            switch (msg) {  
            case WM_DESTROY:  
            {  
                PostQuitMessage(0);  
                return 0;  
                }         
            }  
    

    )


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.