54

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.

default
  • 11,485
  • 9
  • 66
  • 102
  • See also http://stackoverflow.com/questions/7360500/what-are-the-differences-between-redrawwindow-and-updatewindow-in-win32 – cdiggins May 02 '13 at 16:46

4 Answers4

67

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, is a function with a much wider and flexible set of capabilities. It can be used to perform invalidation scheduling (i.e. the same thing InvalidateRect does) or it can be used to forcefully perform immediate redrawing of the specified area, without doing any "scheduling". In the latter case calling RedrawWindow is virtually equivalent to calling InvalidateRect and then immediately calling UpdateWindow.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 1
    An important note is that `RedrawWindow` gives you control over whether children are invalidated. `InvalidateRect` does not give you this control - if you have `WS_CLIPCHILDREN` specified, then it will never invalidate children, otherwise it will invalidate children, but it depends on the exact rectangle that was invalidated, and the rules are very obscure. If you want to invalidate your window with its children, you'll want to use `RedrawWindow` with `RDW_ALLCHILDREN` - forget about `InvalidateRect`. – dialer Mar 01 '21 at 12:16
22

RedrawWindow(win, NULL, NULL, RDW_INVALIDATE); and InvalidateRect(win, NULL, NULL); are equivalent. Both functions invalidate the window. The WM_PAINT occurs at the normal time (no other messages in the application queue) in both cases.

If you want the paint to be done immediately then calling either RedrawWindow(win, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW) or InvalidateRect followed by an UpdateWindow will do that.

RedrawWindow simply gives more options with the RDW_* bits. If all you want is to invalidate the window without the immediate paint then calling InvalidateRect seems cleaner.

Mike T
  • 229
  • 2
  • 3
1

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
  • 43,293
  • 10
  • 75
  • 117
  • 1
    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. – AnT stands with Russia Feb 24 '10 at 17:28
0

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
  • 53,344
  • 14
  • 119
  • 168
  • 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 Feb 24 '10 at 13:08
  • what can be the `other` reason ? – Ashish Negi Apr 28 '15 at 10:05
  • The way I understand it, invalidate rect gives the windows display subsystem the freedom to defer your paint until it's better from the point of view of performance. It allows you as a developer to be humble about not knowing else is going on at the time. If what you are displaying is time critical (an animation frame ?) Vs a static text indicating an email has arrived, might be examples of when to use redrawWindow or invalidaterect, respectively – unsynchronized May 25 '23 at 17:11