1.6.5 WM_PAINT Message

The WM_PAINT message is a request from Windows to a given window to update its display. Windows sends a WM_PAINT message to a window whenever it is necessary to repaint a portion of an application's window. When a window receives a WM_PAINT message, it should retrieve the update region by using the BeginPaint function, and it should carry out whatever operations are necessary to update that part of the client area.

The InvalidateRect and InvalidateRgn functions do not actually generate WM_PAINT messages. Instead, Windows accumulates the changes made by these functions and its own changes while a window processes other messages in its application queue. Postponing the WM_PAINT message lets a window process all changes at once instead of updating bits and pieces in time-consuming individual steps.

A window can require Windows to send a WM_PAINT message by using the UpdateWindow function. The UpdateWindow function sends the message directly to the window, regardless of the number of other messages in the application queue. UpdateWindow is typically used when a window wants to update its client area immediately, such as just after the window is created.

Once a window receives a WM_PAINT message, it must call the BeginPaint function to retrieve the display context for the client area and to retrieve other information such as the update region and whether the background has been erased.

Windows automatically selects the update region as the clipping region of the display context. Since GDI discards (clips) drawing that extends outside the clipping region, only drawing that is in the update region is actually visible. For more information about the clipping region, see Section 2.8, “Clipping Functions.”

The BeginPaint function empties the update region to prevent the same region from generating subsequent WM_PAINT messages.

After completing the painting operation, the window must call the EndPaint function to release the display context.