3.2 How to Write the Hello Program

This section gives an overview of the steps in writing Hello. As you work through the steps, you will learn what files to prepare and how to compile the program.

To write Hello with the Microsoft Foundation Classes, you must:

1.Create an application object.

The application object represents your application and is responsible for application-level initialization. Its most important task is to construct a main window object. For more information on this step, see “Create an Application Object” on page 85.

2.Put a window on the screen.

It takes two steps to display a window with the Microsoft Foundation Classes. First, construct a main window object. Second, during that construction, create a window for display. For more information on this step, see “Put a Window on the Screen” on page 90.

3.Arrange for communication with Windows.

Once a window has been created, it must respond to pertinent Windows messages, such as WM_PAINT or WM_COMMAND. To arrange message processing in your window object, add a “message map” and appropriate “message-handler” functions. For more information on this step, see “Arrange for Communication with Windows” on page 94.

4.Paint the window.

With a window showing, paint the contents of its client area. For Hello, paint the text “Hello, Windows!” For more information on this step, see “Paint the Window” on page 101.

5.Add an About dialog box.

As a final touch, add a dialog box that displays information about the program. The user activates this “About” dialog box by choosing the About command from the Help menu. For more information on this step, see “Add an About Dialog Box” on page 105. For more information on menus and other resources, see the next step.

6.Prepare supporting files.

Windows programs require some supporting files. Add a module-definition file, a resource file, a resource include file, and a makefile. For more information on this step, see “Prepare Supporting Files” on page 107.

7.Build the program.

Once you have prepared all the files, compile and link the program to produce the executable file. Remember that you must run the program from Windows. For more information on this step, see “Build the Program” on page 109.

You will also use these same steps as a foundation for writing your own Windows programs with the Microsoft Foundation Classes.

The sections that follow break these steps into smaller steps. At each step, the related code is presented. After the steps, a Discussion section explains what the code does and why it was done that way. Where appropriate, additional advanced discussion in a special box elaborates upon the code and the basic discussion.