Recall from Hello that the main window object's OnPaint member does all painting in the window, in response to an update, which generates a WM_PAINT message. Of course, Phone Book paints different information in the window, so its OnPaint function is different from Hello's.
OnPaint creates some utility objects, makes some initial calculations, and then loops through the data, drawing the information for each person in the database on its own line in the window.
The utility objects include a CPerson object to store information about the current person being drawn, a CPaintDC device-context object to draw into, and a CBrush object to represent the Windows brush needed to paint the window background. Recall that a CPaintDC object is like a CDC device-context object except that it automatically calls BeginPaint and EndPaint for you.
The function calls the Windows function GetSysColor to get the background color for the window. Then it calls the CreateSolidBrush member function of a CBrush object to create a brush of the right color for the background. This brush is later used to fill areas of the screen.
Initial calculations determine the number of lines from the size of the database and compute the dimensions of the rectangle to paint based on the line height for text in the device context's current font.
The for loop paints lines of text for all person objects in the database. It first computes the horizontal and vertical coordinates (relative to the window) of a rectangle in which the next line is to be drawn. These are used to erase the area for the line by painting it with the background brush object. Then the line is set up and displayed in three parts.
First, the current person is retrieved from the database. The CPerson object's member functions are used to set up a display string containing the person's last and first names, separated by a comma and a space. Member functions of the device-context object are called to align the text and paint it in the appropriate area.
Second, the display string is reconfigured to contain the person's phone number. This is aligned and painted as a second column of information.
Third, the display string is reconfigured to contain the person's modification date. This is aligned and painted as a third column of information.
If the line currently being displayed is the line containing the “selection,” the rectangle containing the line's text is inverted to highlight the selection. Only one person (and one line of the display) can be selected at one time.
When all lines of data have been painted, OnPaint returns.