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.
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.
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.
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!
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.
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.