MessageLoop - Welche Version ist korrekt?



  • Im Netz finde ich verschiedene Arten, wie Leute den MessageLoop programmieren. Um so mehr bin ich inzwischen irritiert, welche Variante wirklich korrekt bzw sauber ist.

    while(GetMessage(&Msg, NULL, 0, 0))
    
        while(GetMessage(&Msg, NULL, 0, 0) != 0)
    
        while(GetMessage(&Msg, NULL, 0, 0) == TRUE)
    

    The above are all wrong!

    It may be of note that I used to use the first of these throughout the tutorial, since as I just mentioned, it works fine as long as GetMessage() never fails, which when your code is correct it won't. However I failed to take into consideration that if you're reading this, your code probably won't be correct a lot of the time, and GetMessage() will fail at some point 🙂 I've gone through and corrected this, but forgive me if I've missed a few spots.

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    

    Quelle: http://www.winprog.org/tutorial/message_loop.html

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nCmdShow)
     {
       MSG msg;
       BOOL bRet;
    
       while((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
       {
         if(bRet == -1)
         {
             // Handle Error
         }
         else
         {
             TranslateMessage(&msg);
             DispatchMessage(&msg);
         }
       }
       return msg.wParam;
     }
    

    Aktuell sieht mein EventLoop wie folgt aus und scheint zu funktionieren:

    While(GetMessage( msg, Null, 0, 0))
                TranslateMessage( msg );
                DispatchMessage( msg );
            Wend
    


  • va!n schrieb:

    Aktuell sieht mein EventLoop wie folgt aus und scheint zu funktionieren:

    Das entspricht den ersten beiden Varianten und ist völlig in Ordnung, siehe auch: When will GetMessage return -1?.


Anmelden zum Antworten