DoEvents equivalent for C++?

前端 未结 2 776
眼角桃花
眼角桃花 2020-12-06 13:42

I\'m new to native c++. Right now, I made it so when I press the left mouse button, it has a for loop that does InvalidateRect and draws a rectangle, and increments X by the

相关标签:
2条回答
  • 2020-12-06 14:35

    DoEvents basically translates as:

    void DoEvents()
    {
        MSG msg;
        BOOL result;
    
        while ( ::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE ) )
        {
            result = ::GetMessage(&msg, NULL, 0, 0);
            if (result == 0) // WM_QUIT
            {                
                ::PostQuitMessage(msg.wParam);
                break;
            }
            else if (result == -1)
            {
                 // Handle errors/exit application, etc.
            }
            else 
            {
                ::TranslateMessage(&msg);
                :: DispatchMessage(&msg);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-06 14:43

    I am a bit rusty in Win32 API, but the asynchronous way of doing this would be:

    • Invalidate the rect
    • Set a timer (see below) to send a message after 50ms
    • Return to the event loop to let WM_PAINT events happen
    • On receiving the timer message, move the rect, then repeat

    This way integrates nicely with being event driven. I realize this is not exactly what you ask for, but I thought I'd mention it as a possible solution anyway :)

    EDIT: A quick google turns up the Windows API call [SetTimer](http://msdn.microsoft.com/en-us/library/ms644906(VS.85,loband).aspx) which you can use to facilitate this. The message will be a WM_TIMER one.

    0 讨论(0)
提交回复
热议问题