What happens if I don't paint when I get a WM_PAINT message?

Raymond Chen

Suppose your window procedure doesn’t paint when it gets a WM_PAINT message. What happens?

It depends on how you don’t paint.

If you have an explicit handler for the WM_PAINT message that does nothing but return without painting, then the window manager will turn around and put a new WM_PAINT message in your queue. “And try harder this time.” Remember that the rules for the WM_PAINT message are that the window manager will generate a WM_PAINT message for any window that has a dirty region. If you fail to remove the dirty region in your WM_PAINT message handler, well, then the rules state that you get another WM_PAINT message. (The most common way of clearing the dirty region is to call Begin­Paint, but there are other less common ways, like Validate­Rect or Redraw­Window with the RDW_VALIDATE flag.)

The other case is that you simply don’t have a WM_PAINT handler and let the message fall through to Def­Window­Proc. In that case, Def­Window­Proc will do a blank paint for you. In other words, Def­Window­Proc contains the logical equivalent of

case WM_PAINT:
  if (BeginPaint(hwnd, &ps))
   EndPaint(hwnd, &ps);
 return 0;

In the case where you pass the WM_PAINT to Def­Window­Proc, the dirty region is cleared because Def­Window­Proc will call Begin­Paint for you.

There are some quirks in the handling of the WM_PAINT message by the Def­Window­Proc function to handle various application compatibility cases, but the above is the basic idea.

To avoid tripping over the weird application compatibility cases, decide up front how you want to deal with WM_PAINT messages delivered to your window procedure.

  • Handle them completely by calling Begin­Paint and End­Paint, then returning 0. (Do not pass the message to Def­Window­Proc.)

  • Pass them all to Def­Window­Proc, and let it do the Begin­Paint and End­Paint.

Don’t try playing fancy games like “Oh, I’m going to call Begin­Paint and End­Paint, but sometimes I’m also going to pass the message to Def­Window­Proc afterwards.” Just pick one plan and stick to it. It’s a lot simpler for everybody that way.