Documents, Views, and the Framework

OverviewHow Do ITutorial

At the heart of the MFC framework are the concepts of document and view. A document is a data object with which the user interacts in an editing session. It is created by the New or Open command on the File menu and is typically saved in a file. (Don't confuse standard MFC documents, derived from class CDocument, with Active documents or OLE compound documents.) A view is a window object through which the user interacts with a document.

The key objects in a running application are:

  • The document(s)

    Your document class (derived from ) specifies your application's data.

    If you want OLE functionality in your application, derive your document class from or one of its derived classes, depending on the type of functionality you need.

  • The view(s)

    Your view class (derived from ) is the user's "window on the data." The view class controls how the user sees your document's data and interacts with it. In some cases, you may want a document to have multiple views of the data.

    If you need scrolling, derive from . If your view has a user interface that is laid out in a dialog-template resource, derive from . For simple text data, use or derive from . For a form-based data-access application, such as a data-entry program, derive from (for ODBC) or (for DAO). Also available are classes , , and .

  • The frame windows

    Views are displayed inside "document frame windows." In an SDI application, the document frame window is also the "main frame window" for the application. In an MDI application, document windows are child windows displayed inside a main frame window. Your derived main frame-window class specifies the styles and other characteristics of the frame windows that contain your views. If you need to customize frame windows, derive from to customize the document frame window for SDI applications. Derive from to customize the main frame window for MDI applications. Also derive a class from to customize each of the distinct kinds of MDI document frame windows that your application supports.

  • The document template(s)

    A document template orchestrates the creation of documents, views, and frame windows. A particular document-template class, derived from class , creates and manages all open documents of one type. Applications that support more than one type of document have multiple document templates. Use class for SDI applications, or use class for MDI applications.

  • The application object

    Your application class (derived from ) controls all of the objects above and specifies application behavior such as initialization and cleanup. The application's one and only application object creates and manages the document templates for any document types the application supports.

  • Thread objects

    If your application creates separate threads of execution — for example, to perform calculations in the background — you'll use classes derived from . itself is derived from CWinThread and represents the primary thread of execution (or the main process) in your application. You can also use MFC in secondary threads.

In a running application, these objects cooperatively respond to user actions, bound together by commands and other messages. A single application object manages one or more document templates. Each document template creates and manages one or more documents (depending on whether the application is SDI or MDI). The user views and manipulates a document through a view contained inside a frame window. The figure Objects in a Running SDI Application below shows the relationships among these objects for an SDI application.

Objects in a Running SDI Application

The rest of this family of articles explains how the framework tools, AppWizard, ClassWizard, WizardBar, and the resource editors, create these objects, how they work together, and how you use them in your programming. Documents, views, and frame windows are discussed in more detail in Window Objects: Overview and Document/View Architecture: Overview.