Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Examining the Document View Architecture

MFC not only provides a set of classes to help you work with document objects, but it provides a complete architecture that ties the various classes together. In this chapter, you will look at this architecture and what it can do for your applications. I think you will find that the whole of the architecture is much greater than the sum of its parts.

Single Versus Multiple Document Applications

If you have used Windows at all, you should be familiar with applications such as Microsoft Word that enable you to work with several documents within the same application. In Windows terms, these are known as multiple document interfaces (MDIs). In addition to the support the Windows Software Development Kit (SDK) provides for MDI, MFC encapsulates MDI in the classes that make up the document view architecture.

The single document interface (SDI) is similar to MDI but is designed for simpler applications that work with only one document and one view at any given time. Both MDI and SDI applications can use the document view architecture provided by MFC in much the same way, except the MDI architecture provides an additional level of functionality to deal with multiple documents and views.

Of course, you are not required to use the document view architecture in your applications, and you most likely will have situations in which you don't even want to have a Windows user interface. You can use MFC in your applications without any GUI interface, or you can base your application on a single dialog box. If your application fits in the document view model, however, you will find that creating your application using either the SDI or MDI flavor of the document view architecture can greatly simplify your task of adding features that Windows users have come to expect from all good Windows applications.

Document Classes

Although it doesn't take much imagination to see how a document object would apply to a word processing or spreadsheet application, the concept of a document in MFC also can apply to much more. In general terms, a document is any set of data that can be grouped logically. This data may include anything from settings for a terminal session to simulation models to information about your favorite records to just about anything else you might consider writing to a file. In MFC, document objects are derived from the CDocument class. In most cases, your application somehow will present the data in your document to the user. This is done by using views.

View Classes

A view object is just that—a view of the document. This may be either some direct representation of the document object or some other sort of display related to the data in your document. For example, your document may store settings for some data-acquisition application, and your views will show the data acquired based on those settings. As you might guess, view objects in MFC are derived from the CView class. However, you probably will derive your view classes from some of the other classes MFC provides for you that add functionality to the basic CView class.

Frames

MFC applications that use the document view architecture use frames to contain the views of the application. In SDI applications, the frame that contains the one and only view also serves as the main frame, or main window, of your application. In MDI applications, this functionality is split between the main frame, which is the main window for your application, and child frames, which provide windows in which each of your views can run. The frame objects handle the menus, status bars, and toolbars for your application. The frame objects also receive the command messages generated in your views. In SDI applications, frame windows are derived directly from the CFrameWnd class, and MDI main frames and child frames are derived from CMDIFrameWnd and CMDIChildWnd, respectively.

Document Templates

MFC uses document templates to tie your documents, views, and frames together. As you will see, it is actually the document template that creates new documents and new view windows to display them. Document templates are derived from class CSingleDocTemplate for SDI apps and from class CMultiDocTemplate for MDI apps.

Share ThisShare This

Informit Network