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 the 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 message 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.

To direct Windows to send a WM_PAINT message, an application can use the UpdateWindow function. The UpdateWindow function sends the message directly to the window, regardless of the number of other messages in the application's message queue. UpdateWindow is typically used when a window needs 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 Chapter 2, “Graphics Device Interface.”

The BeginPaint function clears 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.