3.1.3 Invalidating the Client Area

Windows is not the only source of WM_PAINT messages. The InvalidateRect or InvalidateRgn function can also generate WM_PAINT messages for your windows. These functions mark all or part of a client area as invalid (in need of redrawing). For example, the following statement invalidates the entire client area of the window identified by the hWnd variable:

InvalidateRect(hWnd, NULL, TRUE);

In this example, the NULL argument, used in place of a rectangle structure, specifies the entire client area; the TRUE argument causes the background to be erased.

When the client area is marked as invalid, Windows posts a WM_PAINT message. But if other parts of the client area are marked as invalid, Windows does not post another WM_PAINT message. Instead, it adds the invalidated areas to the previous area, so that all areas are processed by the same WM_PAINT message.

If you do not want your application to redraw the client area, use the ValidateRect and ValidateRgn functions to invalidate only parts of the client area. These functions remove any previous invalidation and will remove the WM_PAINT message if no other invalidated area remains.

If you do not want the application to wait for the WM_PAINT message to be retrieved from the application queue, use the UpdateWindow function to force an immediate WM_PAINT message. If there is any invalid part of the client area, UpdateWindow pulls the WM_PAINT message for the given window from the queue and sends it directly to the window procedure.