You have learned about The Application Class and The Frame Window Class and the roles these classes play in MFC applications. In this section, you will learn about additional classes that are used primarily in document/view applications.
Five classes or objects represent the architectural components of a document/view application. As an MFC developer, you need to understand the function of each of these components and their interrelationships. To view an animation that shows the relationships between the architectural components of a document/view application, click this icon.
This section includes the following topics:
Let's begin our study of document/view with a conceptual overview of the primary objects in its architecture and how they interact with each other. The following illustration shows the document/view architecture for an MFC application.
The application's data is stored in the document object, which is displayed in the view. The view object is a child window sized to fit the frame window and serves as the client area for the parent. The frame window object is the application's top-level window and usually includes a resizing border, a caption bar, a system menu, and Minimize, Maximize, and Close buttons. The arrows in the illustration show the direction of data flow during various application operations.
To view an animation that shows the interaction between the document, view, and frame window objects, click this icon.
You can see that the interaction between these objects is somewhat complex. The interaction will be easier to understand after you learn more about the role each object plays in a running application and write a couple of document/view applications of your own.
In a document/view application, data is stored in a document object represented by a class derived from CDocument. The CDocument class loads, stores, and manages the program's data. It also contains the functions that are used to access and work with the data. To support the close connection between documents and views within the document/view architecture, each document object maintains a list of all the associated views and each view object maintains a pointer to its associated document.
Key Member Functions
A derived CDocument class inherits some important member functions. The following table lists several key CDocument member functions and describes what they do.
Member function Description
GetNextView Returns a CView pointer to the next view in the list of views associated with the document.
GetPathName Retrieves the document's file name and path. Returns a null string if the document has not been named.
GetTitle Retrieves the document's title. Returns a null string if the document has not been named.
IsModified Returns a nonzero value if the document contains unsaved data, or 0 if it does not.
SetModifiedFlag Sets or clears the document's modified flag, which indicates whether the document contains unsaved data.
UpdateAllViews Updates all views associated with the document by calling each view's OnUpdate function.
Overridable Member Functions
The CDocument class includes several overridable functions that you can use to customize a document's behavior. The following table lists several key overridable CDocument member functions and describes what they do.
Member function Description
OnOpenDocument Called by the framework when a document is loaded from disk. Override to initialize the unserialized data members of the document object before a new document is loaded.
DeleteContents Called by the framework to delete the document's contents. Override to free memory and other resources allocated to the document before it is closed.
Serialize Called by the framework to serialize the document to or from a file. Override to provide document-specific serialization code so that your documents can be loaded and saved.
The view object physically represents the client area of the application. Logically, it represents a viewport of the information contained in the document class and allows user input through the mouse or keyboard. While a document object can have any number of views associated with it, a view always belongs to just one document.
The CView class provides the basic framework for output to the view window and the printer, and communicates with the associated document. CView defines the basic properties of the view, and the derived view classes add functionality to the basic definition of a view. Classes derived directly from CView can display information in various ways. They are responsible for supplying all their own painting code.
MFC provides a number of view classes that enable your application to display information in many ways without requiring you to write the underlying painting code. For example, CTreeView and CListView, new with Windows 95, comprise the directory tree and file list views in Explorer. In these extended view classes, the painting functionality is self-contained; in most cases, you need only tell the view class how the painting should appear.
Derived View Classes
The following table describes several classes derived from CView and describes what they do.
View class Description
CEditView Provides the functionality of a Windows edit control and adds print, search, and search-and-replace.
CRichEditView Provides the functionality of a Windows rich edit control.
CListView Provides the functionality of a Windows list view control.
CTreeView Provides the functionality of a Windows tree view control.
CScrollView Adds scrolling functions to a view. Base class for CFormView and CRecordView.
CFormView Implements scrolling views using controls created from a dialog template.
CRecordView Provides views of a database.
Overridable Member Functions
The CView class includes overridable functions that you can use to customize a view's behavior. The following table lists the key overridable CView member functions and describes what they do.
Member function Description
OnDraw Supports Print, Print Preview, and painting on the screen.
OnInitialUpdate Called when a view is first attached to a document. Override to initialize a view of a freshly loaded or created document.
OnUpdate Called when the document's data has changed and the view needs to be updated. Override to implement "as needed" updating, where only the part of the view that has changed is repainted rather than the whole view.
The document template base class, CDocTemplate, is the class that binds together the frame, view, document, and a set of application resources. At least one instance of a document class is dynamically created and maintained by the application class. The document template object maintains a list of all document objects because it is responsible for managing their existence. The template further associates various resources with those objects. In most cases, you do not need to modify the behavior of this class.
SDI and MDI
The framework uses two document template classes: CSingleDocTemplate for SDI applications and CMultiDocTemplate for MDI applications.
A CSingleDocTemplate object provides a single document interface (SDI) and can create and own only one document. An SDI application uses the main frame window to display its document. Only one document can be open at a time.
The CSingleDocTemplate constructor takes four arguments:
® A resource ID identifying the various resources that are linked to the document template
® The run-time class of the CDocument derived class
® The run-time class of the view's frame
® The run-time class of the document's view
A CMultiDocTemplate object can create, own, and manage multiple documents of one document type. It provides a multiple document interface (MDI). An MDI application uses the main frame window as a workspace in which the user can open document frame windows.
No comments:
Post a Comment