The following sections explain how Hello puts a window on the screen.
Previously, you saw the process of constructing a main window object in the InitInstance member function of the application object (“Create an Application Object” on page 85). The next step is to have that window object create a window on the screen—a window with a caption bar, a frame, and various Windows controls.
Part of this work is done in the application object's InitInstance function. After creating the window object with new, InitInstance calls two of the window object's functions to display it. The rest of the work is done by the window object itself.
The CMainWindow class is derived publicly from CFrameWnd. The Microsoft Foundation Class Library provides several other window classes from which you might choose to derive your own window classes, depending on your needs. For information about the choices, see the Class Libraries Reference. CFrameWnd is commonly chosen to represent an application's main window. Figure 3.2, on page 88, shows the class hierarchy for Hello's main window class.
CMainWindow has the following components:
A constructor
Two message-handler member functions
A macro invocation
When InitInstance constructs a main window object, the CMainWindow constructor is invoked. You saw the code for the constructor of CMainWindow above. There are many things you could do in your constructor. Hello uses the constructor to create a window for display. The window is created by a call to the Create member function that CMainWindow inherits from its base class, CFrameWnd, and which CFrameWnd inherits in turn from its own base class, CWnd.
The call to Create creates the window but doesn't make it visible. Create takes the following arguments:
A window class name.
Hello's first argument is NULL. If you pass NULL for this argument, the Microsoft Foundation Classes select an appropriate window class and prepare its data structures. Traditional Windows programmers are accustomed to registering their own window classes with Windows. With the Microsoft Foundation, you can still register classes if you need to, but the most commonly used window classes are preregistered, and the Microsoft Foundation chooses the most appropriate one. For more information about registering window classes, see “How Hello Works.” Note that a “window class” in traditional Windows is not the same thing as a C++ window class, such as CFrameWnd, in the Microsoft Foundation Class Library.
A string specifying caption text for the window.
Hello's second argument is a null-terminated string containing the text to be displayed as a caption in the window's title bar.
A window style.
Hello's third argument is a constant specifying the window style as WS_OVERLAPPEDWINDOW. The WS_OVERLAPPEDWINDOW
style specifies an overlapping window with a caption, a thick-frame border, a system menu, and minimize and maximize boxes. You'll typically pass WS_OVERLAPPEDWINDOW for a main program window, but you can pass any value for this argument that you pass for the corresponding argument in the Windows CreateWindow function. For more information about window styles, see the Windows SDK Reference.
A rectangle specifying where to display the window on the screen.
Hello's fourth argument is rectDefault. If you pass this predefined value instead of your own CRect object, the Microsoft Foundation Classes use a default rectangle specified by Windows. The default position and dimensions of the window depend on the system and on how many other applications have been started. Class CRect is a Microsoft Foundation class designed to represent a two-dimensional rectangle similar to the Windows RECT data type. The class provides member functions to manipulate rectangles in a variety of ways. For more information about CRect, see the Class Libraries Reference.
A pointer to the parent window (of type CWnd), if any.
Hello's fifth argument is NULL. Because this is a main window, not a child window, it has no parent window.
The name of the window's menu resource.
Hello's sixth argument is “MainMenu,” a string that specifies the menu template name used in file HELLO.RC to define Hello's menu.
The CMainWindow constructor also calls the LoadAccelTable member function of class CWnd. Class CMainWindow inherits this member function from CWnd. The call to LoadAccelTable loads a Windows accelerator table, which defines the shortcut keys (also known as accelerator keys) that the program can respond to. The Hello program defines one shortcut key: the F1 key calls up the About dialog box. For more about resources such as accelerator tables, see the Windows SDK Guide to Programming.
CMainWindow's Member Functions and Message-Map Macro
Besides its constructor, class CMainWindow overrides two message-handler member functions, OnAbout and OnPaint, and invokes the DECLARE_MESSAGE_MAP macro. These functions and the macro are discussed further in “Arrange for Communication with Windows” on page 94, in “Paint the Window” on page 101, and in “Add an About Dialog Box” on page 105.
How InitInstance Displays the Window
The main window constructor is invoked when Hello's InitInstance member function allocates a CMainWindow object with new. Once the constructor completes, control returns to InitInstance. At this point, the window is ready for display but still is not visible on the screen.
To display the window, InitInstance calls the newly created window object's ShowWindow member function. You'll recall that class CMainWindow didn't declare a ShowWindow function. Instead, it inherits ShowWindow from CFrameWnd.
ShowWindow makes the window visible, but nothing has yet been painted in the new window's client area. To accomplish that, InitInstance calls the main window object's UpdateWindow member function. CMainWindow inherits this function, like ShowWindow, from CFrameWnd. UpdateWindow causes Windows to send a WM_PAINT message to the window. When the window responds to that message, it paints the text “Hello, Windows!” How the window responds to WM_PAINT is explained in “Paint the Window” on page 101. Figure 3.3 shows the process schematically.
After calling the ShowWindow and UpdateWindow member functions,
InitInstance is finished. At that point, Hello begins its message loop and is
ready to receive messages from Windows.