Discussion: Hello's Application Class

The primary purpose of Hello's application object is to construct a main window object. This section discusses that process further.

The Application Class and the Application Object

Under the Microsoft Foundation Classes, the application class is used to construct an application object. Member functions of that object are called to perform initialization tasks and to run the program's Windows message loop. Typically, your application object creates other objects, such as window objects, that do the application's specific work.

As a class derived publicly from the Microsoft Foundation class CWinApp, your application object inherits the member variables and functions needed to do application-level initialization and to run the message loop. Most of the functions are called automatically when your program runs. You can override any of CWinApp's member functions to customize the program, including InitApplication, ExitInstance, and InitInstance. Typically, however, you'll override only the InitInstance and perhaps ExitInstance member functions of class CWinApp. In Hello, your overriding InitInstance is where you put the code that creates and displays a window.

For more information about CWinApp, see the Class Libraries Reference and Chapter 13 in this book. For more information about what the application object does, see the following sections, especially “How Hello Works” on page 110.

Figure 3.2 shows the class hierarchy for the application class.

InitInstance and the Window Object

When you override InitInstance for Hello, you use the C++ new operator to allocate and initialize a main window object. When the constructor for this object is invoked, it creates the window data structures that Windows needs to display a window on the screen. InitInstance completes the window-creation task by making the window visible and causing it to be painted.

After the CMainWindow constructor returns, InitInstance calls two member functions of the window object:

ShowWindow.

Makes the new window visible.

UpdateWindow.

Causes Windows to send a WM_PAINT message to the window, whose OnPaint member function then paints the contents of its client area. These calls are discussed further in the discussion section following “Put a Window on the Screen” on page 90.

The m_pMainWnd identifier used in InitInstance is a member variable that CTheApp inherits from the CWinApp classs. It stores a pointer to the application's main window object.

Its name reflects a convention of the Microsoft Foundation Class Library for naming member variables. All member variables begin with the “m_” prefix. The rest of the variable's name reflects its purpose and its type, in familiar Windows Hungarian variable-naming fashion. The “p” prefix in m_pMainWnd signifies a pointer. When writing Microsoft Foundation programs, it's useful to follow these conventions.

Once the application is initialized, it must put a window on the screen and cause its initial contents, if any, to be displayed. The following sections continue that process.

More About the Application Object

It's possible to create your window object in some other part of your program, but InitInstance is a logical place to do it because each running copy of your program (recall that Windows can run multiple copies at once) calls InitInstance once to perform initialization for that copy. Your application object is a global object, so like all C++ global objects, it is constructed early in the program initialization process. By the time Windows calls your application's WinMain function, a fully initialized application object exists and its member functions can be called. For more information about this sequence of events, see “How Hello Works” on page 110.

Where is WinMain? Experienced Windows programmers are used to writing this essential function themselves, but the Microsoft Foundation Class Library provides a globally defined WinMain function for you. You don't have to use this version of WinMain, but you'll find that it does what you want most of the time and can be customized as well. For more information about substituting your own version of WinMain or customizing the Microsoft Foundation's version, see “How You Can Customize Your Windows Application” on page 112 and see the cookbook.

Notice that class CTheApp does not declare a constructor. Because there is nothing to do in such a constructor, you can simply rely on the constructor inherited from the base class, CWinApp, to construct your application object. Of course, you could add a constructor to your program's application class if you had something for it to do.

Note that you can't create any windows or make any Windows function calls in the constructor because WinMain hasn't been called yet.