Question

I'm still a beginner in programming GUI using c++ winapi32, and I found something strange. Here's a part of my code:

        InvalidateRect(hwnd,&rect, true);
        //Sleep(delay);
        MessageBox(hwnd, "Blahblah!", "blahblah",MB_OK | MB_ICONINFORMATION);
        InvalidateRect(hwnd,&rect, true);
        //Sleep(delay);
        MessageBox(hwnd, "Blahblah!", "blahblah",MB_OK | MB_ICONINFORMATION);

I use Invalidaterect to repaint a part of my windows, and what I'm trying to do is to repaint the windows every 1 second or so to make the changes apparent to the user's eyes. The strange part is that Sleep seems to not affect my windows if it's not followed by MessageBox command, while in fact I dont want to have any MessageBox command for every repainting because it's too disturbing.

I've tried 1000,2000, even 10000 for the delay. The windoes did freeze with the sleep, but the apparent repainting is only done 1x in the last Sleep command...

This code is a part of, say, void A(). And void A() is called by void B().

       //Message loop
       if(turn == 0)
       {
       B();
       }           

Is there any alternative that I can do to fix this problem?

Oh yeah, I use MS Visual C++ 2008 Express by the way

Thanks in advance for any help regarding this matter :)

Was it helpful?

Solution

The important thing is, that InvalidateRect doesn't draw anything. It only schedules the window content to be drawn (enqueues a WM_PAINT message into the message queue). The actual drawing doesn't happen until you return to the message loop and the window actually gets to process the paint message. So after the last Sleep/MessageBox you finally return from your function to arrive at the message loop again and in the message loop you finally get the WM_PAINT message that represents the actual paint event and process this message to redraw the window's content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top