0

When a part of the window needs to be repainted, the area is marked as invalid, and when I repaint the area (in the WM_PAINT event handler), I should mark the area as valid.

But I don't understand what is the need for marking the area as invalid and then validating it later, I mean why not just do the following: when an area needs to be repainted, just send a WM_PAINT message (without marking the area as invalid) and then just repaint the area (without marking it as valid).

  • Also see http://stackoverflow.com/questions/2325894/difference-between-invalidaterect-and-redrawwindow. – jarmod Sep 08 '15 at 01:16

2 Answers2

1

Marking an area as INVALID lets windows know that it is in need of updating. This is to allow partial paints of the screen instead of just completely updating the draw area every pass.

It's a good idea to only invalidate areas of change in mostly static programs to reduce draw overhead.

Validating them after the draw has completed lets windows know that the area has been refreshed and does not need to be redrawn again.

An example of where this would be useful is when you have the following type of window:

enter image description here

When scrolling through the list boxes or ticking the check boxes it is better to only invalidate that area of the window for redraw rather than redrawing the whole window every time the list box or check box needs to be updated to show the new scroll position or check mark.

Serdalis
  • 10,296
  • 2
  • 38
  • 58
1

Imagine someone's throwing balls at a fence. Do you repaint the fence as many times as balls hit it? No, you repaint the fence if, and only if, a ball hit it since the last time you repainted.

You're suggesting that any time an area would be marked invalid, instead a WM_PAINT message should be sent to the application. The big problem with this is that if the same area got invalidated many times, that would result in many WM_PAINT messages being sent and no way to know that many of them have already been handled. So at the time when you most need high performance (because you're falling behind on drawing) you'd have to repeat the same drawing operations over and over. That doesn't make sense.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • So the first time an area needs to be repainted, it is marked as as invalid, and then if the same area needs to be repainted, Windows checks to see if it is invalid, and if it is, Windows don't do anything. And when the area is repainted and marked as valid, and the area needed to be repainted again, it is again marked as invalid. So instead of sending for example hundreds of `WM_PAINT` messages, only one is sent. Is this correct? –  Sep 08 '15 at 03:13
  • @Tony That's very close. Windows actually checks when the application's message queue is empty. – David Schwartz Sep 08 '15 at 04:59
  • Are you talking about when Windows sends a `WM_PAINT` message? (which is when the message queue is empty) –  Sep 08 '15 at 06:29
  • @Tony That's correct. If the queue is empty when the area is invalidated, the message will be sent immediately. Otherwise, it will be deferred until the queue is empty. – David Schwartz Sep 08 '15 at 06:35
  • Your answer is missing two crucial details: `WM_PAINT` is a low-priority message, and multiple calls to `InvalidateRect`/`InvalidateRgn` are coalesced into a single update region. – IInspectable Sep 08 '15 at 09:36