The MFC Document View Architecture is a framework designed to separate the storage and maintenance of data from the display of data. This is achieved by encapsulating the data within a document class and the presentation specifics within a view class. The advantage of decoupling the view from the application data is of most use in larger applications when there are multiple views and multiple types of data objects to incorporate into an application.

MFC supports two types of document/view applications: single-document and multiple-document interface (SDI) applications. Single Document Interface (SDI) applications have one CFrameWnd class and one view class. Each time a document is loaded a new view is created using the information in the new document.  Multiple Document Interface (MDI) applications have one main frame per application containing one or more child windows. Each time a new document is opened a new child is created under the main application frame window. The main frame window contains and manages the common controls such as the menus and toolbars with each child window containing its own document view. Switching between different child frames results in the frame automatically updating the frame window menu and toolbars to match that associated with the currently active child window view.

An application created with the document/view applications will consist of 4 key class components

CDocument- A document is any data associated with the application. The CDocumet class provides the basic functionality for creating loading, saving and updating document data. Writing or reading data to a storage medium is known as serialization. The serialization objects supplied with MFC provide a standard and consistent interface, relieving the user from the need to perform file operations manually.

CView - The CView Class encapsulates the document's visual presentation rendering an image of the document on the screen or printer and handling user interaction through the view window. MFC provides several variations on the view class and the capabilities of a view class. Further information about these view classes can be found at the following
https://docs.microsoft.com/en-us/cpp/mfc/derived-view-classes-available-in-mfc?view=vs-2019

CMainFrame - The CMainFrame class encapsulates the window frame. It holds common application controls such as menus, toolbar and any other visible objects attached to the frame.

CWinApp - The CWinApp class is the base class from which the MFC programmer derives a Windows application object. The application object provides member functions for initialising and running the application and receives all the event messages before passing them to the CFramwview and CView classes.

Dynamic Creation

In a document view application the view, frames and document objects are created dynamically at run time using the DECLARE_CREATE and IMPLEMENT_DYNCRATE macros.  Once the document view and frame class have been created they can be used as parameters in a runtime class structure which is used as a parameter to create the document template. The newly created template can be added to the list of available document templates available to the application.

Saving and Loading Documents

In a document view program the Serialize member function, defined in the CObject class, is responsible for loading or saving an object’s current state. The Serialize function contains a CArchive argument that is used to read and write the object data. The IsStoring or IsLoading member functions indicate whether Serialize is storing (writing data) or loading (reading data). An application either inserts or retrieves an application object’s data using the insertion operator (<<) or extracts data with the extraction operator (>>). 

Example

In the following example an SDI application is created which places a sequence of markers at the position of the mouse click. Selecting the relevant display option will change the display marker from x to asterisk and vice versa

mfc-SDI-image

Display Code Download Code