A General View

When Hello runs, the C++ code creates the theApp object, an object of your application class, for example CTheApp.

After the application object is constructed, Windows calls the WinMain function. WinMain performs some initialization chores. Then it calls your InitInstance. Finally, it starts the message loop.

Typically, as in Hello, you'll use the call to your application object's over-riding InitInstance member function to construct your main window. You accomplish this by constructing a main window object of your main window class, such as CMainWindow. Then you call three member functions of class CWnd, such as CreateWindow, UpdateWindow, and ShowWindow. CreateWindow is called from the constructor of your main window object. The other functions are called from InitInstance.

The call to CreateWindow creates the Windows data structures for a window. The call to UpdateWindow causes Windows to send a WM_PAINT message to the window procedure associated with your window as soon as the message loop starts. The call to ShowWindow causes the window to appear on the screen.

When the message loop starts, the window object's message map is used to call its OnPaint member function, which paints the string “Hello, Windows!” in the window. Then the message loop continues until a WM_QUIT message causes the loop to end and the program to terminate.

Figure 3.7 shows the sequence of events when a Microsoft Foundation Windows application runs.