Most Windows programs call the function UpdateWindow during initialization in WinMain shortly before entering the message loop. Windows takes this opportunity to send the window procedure its first WM_PAINT message. That message informs your window procedure that the client area is ready to be painted. Thereafter, that window procedure should be ready at any time to process additional WM_PAINT messages and even repaint the entire client area of the window if necessary. A window procedure receives a WM_PAINT message whenever one of the following occurs:
A previously hidden area of the window is brought into view when a user moves a window or uncovers a window.
The user resizes the window (if the window class style has the CS- HREDRAW and CS_VREDRAW bits set).
The program uses the ScrollWindow function to scroll part of its client area.
The program uses the InvalidateRect or InvalidateRgn function to explicitly generate a WM_PAINT message.
In some cases in which part of the client area is temporarily written over, Windows attempts to save an area of the display and restore it later. This is not always successful. Windows may sometimes post a WM_PAINT message when:
Windows removes a dialog box or message box that was overlaying part of the window.
A menu is pulled down and then released.
In a few cases, Windows always saves the area of the display it overwrites and then restores it. This is the case whenever:
The cursor is moved across the client area.
An icon is dragged across the client area.
Dealing with WM_PAINT messages requires that you alter your thinking about how you write to the display. Your program should be structured so that it accumulates all the information necessary to paint the client area but paints only ”on demand“—when Windows sends the window procedure a WM_PAINT message. If your program needs to update its client area, it can force Windows to generate this WM_PAINT message. This may seem a roundabout method of displaying something on the screen, but the structure of your programs will benefit from it.