This section discusses the OnCreate and OnSize message-handler member functions. The OnClose member function was discussed earlier in “Add Message Handlers for File Menu Commands” on page 205.
When the window is initially created, Windows sends it a WM_CREATE message. This invokes the main window object's OnCreate member function. In Hello, there was nothing special to do for a WM_CREATE message, but Phone Book uses the message to set its variables for text drawing in the main window's OnPaint member function and for scroll-bar initialization. The member function initializes member variables that store the text metrics information and the current scroll size information.
As defined for the CMainWindow class, OnCreate uses a Windows TEXTMETRIC structure and a Foundation device-context object of class CDC. The device-context object is constructed on the frame of the function, then initialized by a call to the Windows function GetDC. This creates a Windows device context for the screen, which provides information on fonts and text measurements needed for drawing text in the window.
The OnCreate member function calls the device-context object's GetTextMetrics member function to fill the TEXTMETRIC structure with information. Then it releases the device context by calling the Windows function ReleaseDC. Using the text information structure, the function sets its variables. These are member variables declared in class CMainWindow.
As a local variable, the device-context object is destroyed when the function returns.
When Phone Book's window is resized, Windows sends the window a WM_SIZE message. The window object's OnSize member function responds to this message by recalibrating the scroll bars to fit the resized window. OnSize is also called explicitly whenever the size of the data list changes. This occurs, for example, in OnDelete, OnAdd, OnFind, and OnFindAll.
Windows passes the new height and width of the window to the OnSize function. OnSize uses these values to reset its client area size values and to adjust the size of its scroll bars. Two CWnd member functions, SetScrollRange and SetScrollPos, are used to calibrate the scroll bars. Then the window's client area is invalidated to force an update and repaint the window, including its scroll bars.
Note:
The program doesn't use the Microsoft Foundation Class CScrollBar for its scroll bars because, for a main window, Windows automatically supplies scroll bars as needed. Class CScrollBar is a control class used to add scroll bars to a child window enclosed by a main window.