A More Detailed View

When Hello executes, two key actions occur. First, C++ static and global objects, such as Hello's theApp global application object, are constructed. A global variable in the class library is set to point to the application object. Second, when Windows initialization completes, Windows calls the WinMain function. The Microsoft Foundation Classes supply a WinMain function that does the traditional tasks of any Windows WinMain function. The function is responsible for application and instance initialization and for running the application's message loop.

The WinMain Function

As part of its own initialization code, WinMain checks the hPrevInstance argument passed to it by Windows. If hPrevInstance is NULL, WinMain calls the application object's InitApplication member function to perform first-time initialization.

Then, regardless of the value of hPrevInstance, WinMain calls the application object's InitInstance member function to perform extra initialization for this particular program instance. In Hello, the version of InitInstance that WinMain calls is the version defined in class CTheApp as an override of InitInstance.

After initialization, WinMain calls the application object's Run member function to begin the message loop. As Windows interacts with the user, it detects mouse clicks, keystrokes, and other events. It places messages corresponding to these events in an application message queue. The message loop retrieves messages from the application's message queue.

Window Class Registration

In a traditional Windows WinMain function, one important initialization task is to register one or more “window classes.” Windows uses the registration information when it creates specific windows to display on the screen.

Note that a window class in Windows is not a C++ object class, as are the window classes derived from class CWnd in the Microsoft Foundation Classes.

A Windows application written with the Microsoft Foundation Classes registers several standard window classes for you. You can then simply create windows based on the registered classes. However, it is also possible to register your own custom window classes if you need something special.

Hello works nicely with the default window class registrations supplied by the Microsoft Foundation Class Library, so you do not need to do your own window registration.