tags:

views:

1078

answers:

3

When I want to redraw a window, is there any preferred function to call between InvalidateRect and RedrawWindow?

For instance, are these two calls equal: (win would be a HWND)
RedrawWindow(win, NULL, NULL, RDW_INVALIDATE);
InvalidateRect(win, NULL, NULL);

The main question(s): When should I use one or the other? Are there any differences that happen in the background? (different WM_messages / focus / order / priorities..)

The reason that I want to redraw the window is because I send a new image to it that I want it to display, meaning the content of the window is no longer valid.

+1  A: 

I don't like just giving links, but the MSDN gives you all the information you need and it would be a waste of time to re-type it all here.

RedrawWindow

InvalidateRect

In short, yes there are differences. The question is, why do you want to redraw the window? Is it because the contents are no longer valid? If so, use InvalidateRect, otherwise use RedrawWindow.

Peter Alexander
I've been to MSDN and checked the functions, but the conclusion I make is that they end up sending a WM_PAINT to the window by invalidating a rectangle. Answer to your question would be Yes, the content is invalid (It's an image of the desktop). I send an image to the window in a constant rate and then send an InvalidateRect to the window so that it gets updated - Might I ask what another reason could be?
Default
*Added the answer to your question to my main question as well*
Default
A: 

RedrawWindow repaints the window immediately. InvalidateRect only marks the window to be repainted on the next WM_PAINT message. But WM_PAINT messages have lower priority than other messages, so the repainting won't be immediately if your app is busy handling other messages.

Stefan
It is probably worth noting that all "redrawing" requires a `WM_PAINT` message. Thre's no way around it. `RedrawWindow` will also send a `WM_PAINT` to your window. The difference is that `RedrawWindow` will send it in such a way that it is processed immediately.
AndreyT
+3  A: 

InvalidateRect does not immediately redraw the window. It simply "schedules" a future redraw for a specific rectangular area of the window. Using InvalidateRect you may schedule as many areas as you want, making them accumulate in some internal buffer. The actual redrawing for all accumulated scheduled areas will take place later, when the window has nothing else to do. (Of course, if the window is idle at the moment when you issue the InvalidateRect call, the redrawing will take place immediately).

You can also force an immediate redraw for all currently accumulated invalidated areas by calling UpdateWindow. But, again, if you are not in a hurry, explicitly calling UpdateWindow is not necessary, since once the window is idle it will perform a redraw for all currently invalidated areas automatically.

RedrawWindow, on the other hand, performs an immediate redrawing on the specified area, without doing any "scheduling".

So, one can say that calling RedrawWindow is virtually equivalent to calling InvalidateRect and then immediately calling UpdateWindow.

AndreyT
Exactly what I was looking for. Thanks Andrey
Default