Chapter 2

Using AppWizard to Create an MFC Program


At this point, you've got a vague idea of what MFC is, and you're undoubtedly anxious to dig in, get to work, and see what all of this stuff can do for you. What you might not know at this point is that there are two approaches you can take when creating an MFC program. The first approach is to write all of the source code from scratch, just as you're used to doing with your past programs. This first approach is the one you'll be using for most of this book because it gives you a chance to really dig into MFC and see what makes it tick.

The second approach you can take when creating an MFC program is to let Visual C++'s excellent wizards, especially AppWizard, do a lot of the work for you. AppWizard can greatly speed program development by automatically creating a skeleton application that you can build upon to create your own specific application. Because AppWizard can be an important part of using MFC, this chapter, and the next, focus on the basics of using AppWizard to write a Windows application.

Understanding Wizards

If you've used any Microsoft products recently, you're probably already familiar with wizards and what they do. Most of Microsoft's products are loaded with wizards, and nothing would make Microsoft happier than to see other companies also feature wizards in their applications. In fact, wizards are a suggested part of a Windows 95 application and are supported by MFC.

If you're not familiar with the term "wizard" yet, a definition is in order. A wizard is simply an automated task, kind of a fancy macro. A Word for Windows user, for example, might use a letter wizard to start a letter. The wizard guides the user, step-by-step, through the process of creating the letter, requesting information in dialog boxes and finally creating a blank letter for the user to fill in with her text.

Visual C++ has its share of wizards, too. The two most important are AppWizard and ClassWizard. AppWizard guides you through the creation of a "vanilla" MFC application. After you have this skeleton application built, you add your own code to make the application do what you want it to do. ClassWizard, on the other hand, helps you manage the many classes, data members, and member functions in your program. In this chapter, you'll see how to use AppWizard to build a Windows 95 application. In the following chapter, “The Client/Server Quandary,” you'll also get some experience with ClassWizard.

Creating Your First MFC Program

I could spend dozens of pages trying to describe how helpful wizards are. When I was done, you still wouldn't realize how powerful wizards can be. Seeing is believing, so in this chapter you create an MFC program using AppWizard. The final application will feature all of the goodies you're used to seeing on a full-fledged Windows 95 application, including a docking toolbar, a status bar, a menu bar, an About dialog box, and more. Just crank up Visual C++, and then follow these steps to create your first MFC program.

  1. Select File, New from Developer Studio's menu bar. The New property sheet appears, as shown in Figure 2.1.

    FIG. 2.1

    The New property sheet enables you to select the type of files you want to start.
  2. Make sure MFC AppWizard (exe) is selected in the left hand pane. Then type app1 into the Project Name box and the path of the folder into which you want the project files stored into the Location box. (If you like, you can use the Browse button to locate the folder into which you want the files stored.)
  3. Click the OK button. The MFC AppWizard [nd] Step 1 dialog box appears (Figure 2.2). On this dialog box, you can choose to create an SDI, MDI, or dialog-based application. You can also choose a language.

    FIG. 2.2

    In the Step 1 dialog box, you select the type of application you want to build.
  4. Leave the default options selected, and click the Next button. The MFC AppWizard [nd] Step 2 of 6 dialog box appears, as shown in Figure 2.3. If you were creating a database application, you could now choose the type of database support you needed.

    FIG. 2.3

    The Step 2 dialog box enables you to select database support.
  5. Leave the database support set to None, and click the Next button. The MFC AppWizard [nd] Step 3 of 6 dialog box appears, as shown in Figure 2.4. If you were creating an OLE application, you could select OLE support from the given options on this page of the wizard.

    FIG. 2.4

    You use the Step 3 dialog box to select OLE support options.
  6. Accept the default OLE options (None) by clicking the Next button. The MFC AppWizard [nd] Step 4 of 6 dialog box appears (Figure 2.5). On this page of the wizard, you select the features you want to include in your application, including a toolbar, a status bar, and printing and messaging abilities.

    FIG. 2.5

    You use the Step 4 dialog box to select your application's main features.
  7. Leave the default features selected and click the Next button. The MFC AppWizard [nd] Step 5 of 6 dialog box appears (Figure 2.6). Here, you can choose whether the AppWizard-generated source code will include comments and whether the MFC library should be loaded as a DLL (shared) or linked directly into your application's executable file (static).

    FIG. 2.6

    In Step 5, you can choose to include AppWizard-generated comments in the source code, as well as select how your application will use the MFC libraries.
  8. Accept the default settings by clicking the Next button. The MFC AppWizard [nd] Step 6 of 6 dialog box appears (Figure 2.7), which lists the classes that AppWizard is about to create for you. Although you can change the classes' names, you'll usually leave them as they are.

    FIG. 2.7

    The Step 6 dialog box gives you a chance to rename classes.
  9. Leave the classes named as they are, and click the Finish button. The New Project Information dialog box appears, as shown in Figure 2.8. This dialog box displays a summary of the application you've decided to build.

    FIG. 2.8

    The New Project Information dialog box summarizes the selections you've made.
  10. Click the OK button to create your new MFC application. AppWizard generates the source code for the application you selected. When AppWizard is finished, Developer Studio's window will look something like Figure 2.9.

    FIG. 2.9

    After MFC creates your application's source code, Developer Studio's main window will display a pane containing your project.
  11. Click the Build button on the project toolbar or select Build, Build from the menu bar. Developer Studio compiles and links your new MFC application.

Running Your First MFC Application

And that's all there is to it! After Developer Studio finishes compiling and linking the application, select Build, Execute from the menu bar to run the program. When you do, you see the window shown in Figure 2.10. Pretty darn good looking application for a few mouse clicks, no? Although the preceding steps might have seemed long as you were working through them, you can build the application shown in Figure 2.10 in easily less then 30 seconds. Wizardry, indeed!


FIG. 2.10

With AppWizard and a few mouse clicks, you can create an impressive application skeleton.

Except for the fact that your new application doesn't process any kind of data, it's surprisingly complete. You can, for example, create new document windows simply by clicking the toolbar's New button or by selecting File, New from the menu bar. If you select Help, About App1, the application's About dialog box appears. You can (after closing the About dialog box) grab the toolbar with your mouse pointer and drag it somewhere else in the window, changing it from a toolbar to a toolbox. If you select the File, Open command, the Open dialog box appears, enabling you to choose a file to load. Although the file won't actually load, if you select a file, a new document window will appear with the file's name in the document window's title bar. Your new application even features cool stuff like tool tips those little hint boxes that appear when you leave the mouse pointer over a toolbar button for a second or two.

Go ahead and take a few minutes now to explore your new AppWizard-generated MFC application. Try all of the stuff in the previous paragraph, as well as experiment with other commands like File, Print, and File, Print Preview. And don't forget the commands on the View menu, which enable you to add or remove the control bars, or the handy commands in the Window menu, which arrange the document windows in various ways. When you're done playing, meet me at the next section.

Exploring AppWizard's Files and Classes

If you take a quick look into your project's folder, you'll discover that AppWizard created a whole slew of source code and data files for your application. What files appear in your project folder depends on the selections you made when you ran AppWizard. If you carefully followed the steps for creating the application, AppWizard generated a folder called RES that contains some of your project's resources, as well as a folder called Debug or Release (depending on whether you're creating a debugging or release version of the program) that contains all of the project's output files. It also generates 18 other files that make up the application's source code. The files AppWizard generates for the app1 project are the following:

File Name Description
MAINFRM.CPP The frame window class's implementation
MAINFRM.H The frame window class's header file
README.TXT Project description
RESOURCE.H Resource header file
APP1DOC.CPP The document class's implementation
APP1DOC.H The document class's header file
APP1.CLW ClassWizard data file
APP1.CPP The application class's implementation
APP1.H The application class's header file
APP1.DSW The Project's workspace file
APP1.RC The application's main resource file
APP1VIEW.CPP The view class's implementation
APP1VIEW.H The view class's header file
CHILDFRM.CPP The MDI child window class's implementation
CHILDFRM.H The MDI child window class's header file
STDAFX.CPP Precompiled header file
STDAFX.H Precompiled header file

At this point, you might be somewhat confused about what all of the different classes in the app1 application do. Here's a description. The CApp1App class (declared and defined in the APP1.H and APP1.CPP files) represents the program's application object, as derived from MFC's CWinApp. Every Visual C++ MFC program must have an application object.

The CMainFrame class (MAINFRM.H and MAINFRM.CPP) represents the application's frame window. The frame window is the main window you see when you start the application.

The CChildFrame class, derived from CMDIChildWnd, represents the application's child document windows. One of these windows appears whenever you select the New or Open commands.

The CApp1Doc class (APP1DOC.H and APP1DOC.CPP) represents the application's document class and is derived from MFC's CDocument. In a Visual C++ program, the document class holds the data that makes up the current document, such as the text in a word processor document.

Finally, the CApp1View class (APP1VIEW.H and APP1VIEW.CPP) represents the application's view. In an AppWizard MFC program, the view is responsible for displaying the data stored in the document class. The view also enables the user to edit the document.

As you work with MFC applications, you'll better understand how you use the different classes to create the kind of application you want. In fact, in Chapter 3, “The Client/Server Quandary,” you'll learn to modify the various AppWizard-generated classes to create a simple application that enables the user to draw objects in a window and then save the window's contents into a document file for later loading. Along the way, you'll learn about MFC's document/view architecture.