Forcing an update of a window

This is a discussion on Forcing an update of a window within the Windows Programming forums, part of the Platform Specific Boards category; What I'm trying to do:
I have a seperate window that displays parts of a bitmap to make a map. ...

Forcing an update of a window

What I'm trying to do:
I have a seperate window that displays parts of a bitmap to make a map. Image is attached:http://www.mikemill.org/images/map.JPG
I'm using the reverse video to show where the person is.
I've gotten all that to work. My problem is that only way I've been able to get the window to refresh itself is to use two ShowWindow commands to hide and then show. I've tried using SendMessage with a WM_PAINT but it won't redraw it.

Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

GetClientRect(hWnd,&Rect);
InvalidateRect(hWnd,&Rect,FALSE); // tell windows this is the area that needs a redraw and do not erase the background
UpdateWindow(hWnd); // send WM_PAINT
//in your WM_PAINT (this should be ALL you do here if you want a fast update)
GetUpdateRect(hWnd, &theRect, 0);
if (IsRectEmpty(&theRect))
GetClientRect(hWnd,&theRect);
hdc=BeginPaint(hWnd); // validate the update region and get its HDC
// bitblt your screen buffer
EndPaint(hWnd); // clean up

Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

>>>
Passing NULL as the second parameter to InvalidateRect automatically invalidates the whole window for you.
<<<

Exactly, and in a simple application this isn't a problem, however, if you have a complex application, redrawing the entire screen when potentially only a couple of pixels have changed will give you a recognisable performance hit.

Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

UpdateWindow() immediately creates and processes a WM_PAINT message for the current invalid region. From MSDN: "The function sends a WM_PAINT message directly to the window procedure of the specified window, bypassing the application queue. If the update region is empty, no message is sent."

InvalidateRect() creates an invalid region. It places a WM_PAINT message in the queue.

This is why UpdateWindow() by itself doesn't do anything. If there's nothing invalid, then it doesn't update anything.

Also, InvalidateRect() may appear to work, but considering that WM_PAINT is a low priority message, it does not get called immediately. It is called very quickly most of the time, but not when there's a bunch of other things to do.

The solution?

Call them both. InvalidateRect() first, then UpdateWindow(). Also, adrianxw is right - you should not invalidate portions of the window that need not be updated. No sense wasting CPU time for things that does not have to do, especially when you care about speed.