CDocument Class

Provides the basic functionality for user-defined document classes.

class CDocument : public CCmdTarget


A document represents the unit of data that the user typically opens with the File Open command and saves with the File Save command.

CDocument supports standard operations such as creating a document, loading it, and saving it. The framework manipulates documents using the interface defined by CDocument.

An application can support more than one type of document; for example, an application might support both spreadsheets and text documents. Each type of document has an associated document template; the document template specifies what resources (for example, menu, icon, or accelerator table) are used for that type of document. Each document contains a pointer to its associated CDocTemplate object.

Users interact with a document through the CView object(s) associated with it. A view renders an image of the document in a frame window and interprets user input as operations on the document. A document can have multiple views associated with it. When the user opens a window on a document, the framework creates a view and attaches it to the document. The document template specifies what type of view and frame window are used to display each type of document.

Documents are part of the framework's standard command routing and consequently receive commands from standard user-interface components (such as the File Save menu item). A document receives commands forwarded by the active view. If the document doesn't handle a given command, it forwards the command to the document template that manages it.

When a document's data is modified, each of its views must reflect those modifications. CDocument provides the UpdateAllViews member function for you to notify the views of such changes, so the views can repaint themselves as necessary. The framework also prompts the user to save a modified file before closing it.

To implement documents in a typical application, you must do the following:

  • Derive a class from CDocument for each type of document.

  • Add member variables to store each document's data.

  • Implement member functions for reading and modifying the document's data. The document's views are the most important users of these member functions.

  • Override the CObject::Serialize member function in your document class to write and read the document's data to and from disk.

CDocument supports sending your document via mail if mail support (MAPI) is present. See the articles MAPI and MAPI Support in MFC.

For more information on CDocument, see Serialization, Document/View Architecture Topics, and Document/View Creation.


Header: afxwin.h

See Also


MDIDOCVW Sample: Demonstrates MDI Using Doc/View Architecture

SNAPVW Sample: Uses Property Pages in a Form View Application

NPP Sample: Demonstrates the Windows Messaging API (MAPI)


CCmdTarget Class

Hierarchy Chart

CCmdTarget Class

CView Class

CDocTemplate Class

Other Resources

CDocument Members