Document-View Architecture in MFC

PGUI: Document-View Architecture in MFC

The document-view application architecture in MFC includes

  • an application object,
  • a frame window,
  • a document object (or several), and
  • one or more view objects for each document.

The application pumps event messages to the frame window and to the view object. The view object (and the frame window) processes messages and calls the document interface to change the document. The document object send notification of the changes to the view objects.

The document object and the view objects are connected with a document template. The applications InitInstance-function creates the document templates.

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
  IDR_MAINFRAME,
  RUNTIME_CLASS(CDiagramDoc),
  RUNTIME_CLASS(CMainFrame),
  RUNTIME_CLASS(CDiagramView));
AddDocTemplate(pDocTemplate);

The document is connected to the file type with the document template string included in the application resources (with the id IDR_MAINFRAME).

"Diagram\n\nDiagram\nDiagram Files (*.dgr)\n.dgr\n
Diagram.Document\nDiagram Document"

The frame window routes user commands to the different parts of the application.

Example program

The example program has been created with the help of the MFC AppWizard. The whole initial creation process is described in a separate page.

Diagram application

The complete example is available in the Diagram Example -page.

Document

In the example: Node.h, Node.cpp DiagramDoc.h, and DiagramDoc.cpp

The document object holds the document data. In the example program the documents data is represented with two helper classes Node and Connector.

The document class inherits CDocument base class.

The example program uses CArray template class to store the nodes and the connectors.

CArray<Node*, Node*> m_nodes;
CArray<Connector*, Connector*> m_connectors;

The most important member functions (operations) of CDocument are

FunctionDescription
UpdateAllViews Updates all views associated with the document by calling OnUpdate for each view.
IsModified Returns true if the document contains unsaved data.
SetModifiedFlag Sets or clears the document's modified flag, which indicates whether the document contains unsaved data.

The most important overridable members functions are

FunctionDescription
Serialize Called by the framework to serialize (write) the contents of the document into a disk file.
DeleteContents Called by the framework to delete the contents of the document.

 

View

In the example: DiagramView.h and DiagramView.cpp.

The view class includes the functionality of both view and controller of MVC. It processes user input and also presents the document to the user.

The view class inherits the CView base class. It is the simplest view class in MFC.

The view gets a reference to the document with a wizard-generated member function GetDocument.

CDiagramDoc* CDiagramView::GetDocument()
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDiagramDoc)));
	return (CDiagramDoc*)m_pDocument;
}

The most important overridable member functions of CView are

FunctionDescription
OnUpdate Called when the document has changed. The default implementation invalidates the whole view.
OnInitialUpdate Called when the view is first attached to the document.
OnDraw Called to draw the document contents.

 

See also:
Using AppWizard
Model-View-Controller
Diagram example