Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning Visual C++ 2005 (2006) [eng]-1

.pdf
Скачиваний:
108
Добавлен:
16.08.2013
Размер:
18.66 Mб
Скачать

12

Windows Programming with the Microsoft Foundation Classes

In this chapter, you start down the road of serious Windows application development using the MFC. You’ll get an appreciation of what code the Application wizard generates for a Microsoft Foundation Class (MFC) program and what options you have for the features to be included in your code.

In this chapter, you will learn about:

The basic elements of an MFC-based program

How Single Document Interface (SDI) applications and Multiple Document Interface (MDI) applications differ

How to use the MFC Application wizard to generate SDI and MDI programs

What files are generated by the MFC Application wizard and what their contents are

How an MFC Application wizard-generated program is structured

The key classes in an MFC Application Wizard-generated program, and how they are interconnected

The general approach to customizing an MFC Application wizard-generated program

You’ll be expanding the programs that you generate in this chapter by adding features and code incrementally in subsequent chapters. You will eventually end up with a sizable, working Windows program that incorporates almost all the basic user interface programming techniques you will have learned along the way.

Chapter 12

The Document/View Concept in MFC

When you write applications using MFC, it implies acceptance of a specific structure for your program, with application data being stored and processed in a particular way. This may sound restrictive, but it really isn’t for the most part, and the benefits in speed and ease of implementation you gain far outweigh any conceivable disadvantages. The structure of an MFC program incorporates two applicationoriented entities — a document and a view — so let’s look at what they are and how they’re used.

What Is a Document?

A document is the name given to the collection of data in your application with which the user interacts. Although the word document seems to imply something of a textual nature, a document isn’t limited to text. It could be the data for a game, a geometric model, a text file, a collection of data on the distribution of orange trees in California or, indeed, anything you want. The term document is just a convenient label for the application data in your program, treated as a unit.

You won’t be surprised to hear that a document in your program is defined as an object of a document class. Your document class is derived from the CDocument class in the MFC library, and you’ll add your own data members to store items that your application requires, and member functions to support processing of that data. Your application is not limited to a single document type; you can define multiple document classes when there are several different kinds of document involved in your application.

Handling application data in this way enables standard mechanisms to be provided within MFC for managing a collection of application data as a unit and for storing and retrieving data contained in document objects to and from disk. These mechanisms are inherited by your document class from the base class defined in the MFC library, so you get a broad range of functionality built in to your application automatically, without having to write any code.

Document Interfaces

You have a choice as to whether your program deals with just one document at a time, or with several. The Single Document Interface, abbreviated as SDI, is supported by the MFC library for programs that only require one document to be open at a time. A program using this interface is referred to as an SDI application.

For programs needing several documents to be open at one time, you can use the Multiple Document Interface, which is usually referred to as MDI. With the MDI, as well as being able to open multiple documents of one type, your program can also be organized to handle documents of different types simultaneously with each document displayed in its own window. Of course, you need to supply the code to deal with processing whatever different kinds of documents you intend to support. With an MDI application, each document is displayed in a child window of the application window. You have an additional application variant called the multiple top-level document architecture where each document window is a child of the desktop.

650

Windows Programming with the Microsoft Foundation Classes

What Is a View?

A view always relates to a particular document object. As you’ve seen, a document contains a set of application data in your program, and a view is an object which provides a mechanism for displaying some or all of the data stored in a document. It defines how the data is to be displayed in a window and how the user can interact with it. Similar to the way that you define a document, you’ll define your own view class by deriving it from the MFC class CView. Note that a view object and the window in which it is displayed are distinct. The window in which a view appears is called a frame window. A view is actually displayed in its own window that exactly fills the client area of a frame window. Figure 12-1 illustrates a document with two views.

Document

Sales by Month

January

300

February

400

March

400

April

500

May

400

June

400

July

500

August

300

September 500

October

600

November

700

December

800

View 1

800

400

J F M A M J J A S O N D

View 2

800

400

J F M A M J J A S O N D

Figure 12-1

In the example in Figure 12-1, each view displays all the data that the document contains in a different form, although a view could display just part of the data in a document if that’s what’s required.

A document object can have as many view objects associated with it as you want. Each view object can provide a different presentation of the document data or a subset of the same data. If you were dealing with text, for example, different views could be displaying independent blocks of text from the same document. For a program handling graphical data, you could display all of the document data at different scales in separate windows, or in different formats, such as a textual representation of the elements that form the image. Figure 12-1 illustrates a document that contains numerical data — product sales data by month, where one view provides a bar chart representation of the sales performance and a second view shows the data in the form of a graph.

651

Chapter 12

Linking a Document and Its Views

MFC incorporates a mechanism for integrating a document with its views, and each frame window with a currently active view. A document object automatically maintains a list of pointers to its associated views, and a view object has a data member holding a pointer to the document that it relates to. Each frame window stores a pointer to the currently active view object. The coordination among a document, a view, and a frame window is established by another MFC class of objects called document templates.

Document Templates

A document template manages the document objects in your program, as well as the windows and views associated with each of them. There is one document template for each type of document that you have in your application. If you have two or more documents of the same type, you need only one document template to manage them. To be more specific about the role of a document template, a document template object creates document objects and frame window objects and views of a document are created by a frame window object. The application object that is fundamental to every MFC application creates the document template object itself. Figure 12-2 shows a graphical representation of these interrelationships.

Application Object

Pointer to:

Creates

Document Template

Pointer to:

Creates

Creates

Document Object

Pointer to:

Frame Window

Pointer to:

Creates

View Object

Pointer to:

Figure 12-2

652

Windows Programming with the Microsoft Foundation Classes

The diagram uses dashed arrows to show how pointers are used to relate objects. These pointers enable function members of one class object to access the public data or the function members in the interface of another object.

Document Template Classes

MFC has two classes for defining document templates. For SDI applications, the MFC library class CSingleDocTemplate is used. This is relatively straightforward because an SDI application has only one document and usually just one view. MDI applications are rather more complicated. They have multiple documents active at one time, so a different class, CMultiDocTemplate, is needed to define the document template. You’ll see more of these classes as we progress into developing application code.

Your Application and MFC

Figure 12-3 shows the four basic classes that are going to appear in virtually all your MFC-based Windows applications:

The application class CMyApp

The frame window class CMyWnd

The view class CMyView, which defines how data contained in CMyDoc is to be displayed in the client area of a window created by a CMyWnd object

The document class CMyDoc defining a document to contain the application data

The actual names for these classes are specific to a particular application, but the derivation from MFC is much the same, although there can be alternative base classes, particularly with the view class. As you’ll see a bit later, MFC provides several variations of the view class that provide a lot of functionality prepackaged for you, saving you lots of coding. You normally don’t need to extend the class that defines a document template for your application, so the standard MFC class CSingleDocTemplate usually suffices in an SDI program. When you’re creating an MDI program, your document template class is

CMultiDocTemplate, which is also derived from CDocTemplate.

The arrows in the diagram point from a base class to a derived class. The MFC library classes shown here form quite a complex inheritance structure, but in fact these are just a very small part of the complete MFC structure. You need not be concerned about the details of the complete MFC hierarchy in the main, but it is important to have a general appreciation of it if you want to understand what the inherited members of your classes are. You will not see any of the definitions of the base classes in your program, but the inherited members of a derived class in your program are accumulated from the direct base class, as well as from each of the indirect base classes in the MFC hierarchy. To determine what members one of your program’s classes has, you therefore need to know from which classes it inherits. After you know that, you can look up its members using the Help facility.

Another point you don’t need to worry about is remembering which classes you need to have in your program and what base classes to use in their definition. As you’ll see next, all of this is taken care of for you by Visual C++ 2005.

653

Chapter 12

Microsoft

 

CObject

 

Foundation

 

 

 

Classes

 

 

 

 

CCmdTarget

 

CWinThread

 

CWnd

CDocument

 

 

 

CDocTemplate

CWinApp

 

 

 

 

CFrameWnd

CView

 

 

 

 

CSingleDocTemplate

CMyApp

CMyWnd

CMyView

CMyDoc

 

Your Application Classes

 

Figure 12-3

 

 

 

654

Windows Programming with the Microsoft Foundation Classes

Creating MFC Applications

You use four primary tools in the development of your MFC-based Windows programs:

1.You use an Application wizard for creating the basic application program code when you start. You use an application wizard whenever you create a project that results in code being automatically generated.

2.You use the project context menu in ClassView to add new classes and resources to your project. You display this context menu by right-clicking the project name in ClassView and use the Add/Class menu item to add a new class. Resources are things composed of non-executable data such as bitmaps, icons, menus and dialog boxes. The Add/Resource menu item from the same context menu helps you to add a new resource.

3.You use the class context menu in ClassView for extending and customizing the existing classes in your programs. You use the Add/Add Function and Add/Add Variable menu items to do this.

4.You use a Resource Editor for creating or modifying such objects as menus and toolbars.

There are, in fact, several resource editors; the one used in any particular situation is selected depending on the kind of resource that you’re editing. We’ll look at editing resources in the next chapter, but for now let’s jump in and create an MFC application.

The process for creating an MFC application is just as straightforward as that for creating a console program; there are just a few more choices along the way. As you have already seen, you start by creating a new project by selecting the File > New > Project menu item, or you can use the shortcut and press Ctrl+Shift+N. The New Project dialog box is displayed where you can then choose MFC as the project type and MFC Application as the template to be used. You also need to enter a name for the project that can be anything you want — I’ve used TextEditor, as shown in Figure 12-4. You won’t be developing this particular example into a serious application so you can use any name you like.

As you know, the name that you assign to the project — TextEditor, in this case — is used as the name of the folder which contains all the project files, but it is also used as a basis for creating the names for classes that the Application wizard generates for your project. When you click OK in the New Project dialog window, you’ll see the MFC Application Wizard dialog box, where you can choose options for the application, as shown in Figure 12-5.

As you can see, the dialog box explains the project settings that are currently in effect, and on the right of the dialog box you have a range of options you can select. You can select any of these to have a look if you want — you can always get back to the base dialog box for the Application wizard by selecting the Previous button. Selecting any of the options on the right presents you with a whole range of further choices, so there are a lot of options in total. I won’t discuss all of them — I’ll just outline to the ones that you are most likely to be interested in and leave you to investigate the others. Initially, the Application wizard allows you to choose an SDI application, an MDI application, or a dialog box-based application. Let’s create an SDI application first of all and explore what some of the choices are as we go along.

655

Chapter 12

Figure 12-4

Figure 12-5

656

Windows Programming with the Microsoft Foundation Classes

Creating an SDI Application

Select the Application Type option from the list to the right of the dialog window.

The default option selected is Multiple documents, which selects the multiple document interface — MDI, and the appearance of an MDI application is shown top left in the dialog window so that you’ll know what to expect. Select the Single document option and the representation for the application that is shown top-left changes to a single window, as shown in Figure 12-6.

Figure 12-6

Let’s consider some of the other options you have here for the application type:

Option

Description

 

 

Dialog based

The application window is a dialog window rather than a frame

 

window.

Multiple top-level

Documents are displayed in child windows of the desktop

documents

rather than child windows of the application as they are with an MDI

 

application.

Document/View

This option is selected by default so you get code built-in to support

architecture support

the document/view architecture. If you uncheck this option the support

 

is not provided and it’s up to you to implement whatever you want.

Resource language

The drop-down list box displays the choice of languages available that

 

applies to resources such as menus and text strings in your application.

Use Unicode libraries

Support for Unicode is provided through Unicode versions of the MFC

 

libraries; if you want to use them, you must check this option.

 

 

657

Chapter 12

You should uncheck the Use Unicode libraries option that is checked by default. If you leave it checked, the application expects Unicode input and files are stored as Unicode characters. This makes them unreadable in programs that expect ASCII text.

You also can choose between Windows Explorer and MFC standard for the project style. The former implements the application window with the client area divided into two panes; the left pane displays data in the form of a tree and the right pane displays straight text.

You can also choose how MFC library code is used in your program. The default choice of using the MFC library as a shared DLL (Dynamic Link Library) means that your program links to MFC library routines at run-time. This reduces the size of the executable file that you’ll generate, but requires the MFC DLL to be on the machine that’s running it. The two modules together (your application’s .exe module and the MFC .dll) may be bigger than if you had statically linked the MFC library. If you opt for static linking, the MFC library routine is included in the executable module for your program when it is built. Statically linked applications run slightly faster than those that dynamically link to the MFC library so it’s a tradeoff between memory usage and speed of execution. If you keep the default option of using MFC as a shared DLL, several programs running simultaneously using the dynamic link library can all share a single copy of the library in memory.

In the Document Template Strings dialog box, you can enter a file extension for files that the program creates. The extension .txt is a good choice for this example. You can also enter a Filter Name on this dialog box, which is the name of the filter that will appear in Open and Save As dialog boxes to filter the list of files so that only files with your file extension are displayed.

If you select User Interface Features from the list in the right pane of the MFC Application Wizard window you get a further set of options that can be included in your application:

Option

Description

 

 

Thick Frame

This enables you to resize the application window by dragging a border. It

 

is selected by default.

Minimize box

This option is also selected by default and provides a minimize box at the

 

top right of the application window.

Maximize box

This option is also selected by default and provides a maximize box at the

 

top right of the application window.

Minimized

If you select this option the application starts with the window minimized

 

so it appears as an icon.

Maximized

If you select this option the application starts with the window maximized.

Initial status bar

This option adds a status bar at the bottom of the application window con-

 

taining indicators for CAPS LOCK, NUM LOCK, and SCROLL LOCK and

 

a message line that displays help strings for menus and toolbar buttons.

 

The option also adds menu commands to hide or show the status bar.

Split window

This option provides a splitter bar for each of the applications main views.

 

 

658