Relationships Among MFC Objects

To help put the document/view creation process in perspective, consider a running program: a document, the frame window used to contain the view, and the view associated with the document.

  • A document keeps a list of the views of that document and a pointer to the document template that created the document.

  • A view keeps a pointer to its document and is a child of its parent frame window.

  • A document frame window keeps a pointer to its current active view.

  • A document template keeps a list of its open documents.

  • The application keeps a list of its document templates.

  • Windows keeps track of all open windows so it can send messages to them.

These relationships are established during document/view creation. The following table shows how objects in a running program can access other objects. Any object can obtain a pointer to the application object by calling the global function AfxGetApp.

Gaining Access to Other Objects in Your Application

From object How to access other objects
Document Use GetFirstViewPosition and GetNextView to access the document's view list.

Call GetDocTemplate to get the document template.
View Call GetDocument to get the document.

Call GetParentFrame to get the frame window.
Document frame window Call GetActiveView to get the current view.

Call GetActiveDocument to get the document attached to the current view.
MDI frame window Call MDIGetActive to get the currently active CMDIChildWnd.

Typically, a frame window has one view, but sometimes, as in splitter windows, the same frame window contains multiple views. The frame window keeps a pointer to the currently active view; the pointer is updated any time another view is activated.

Note

A pointer to the main frame window is stored in the m_pMainWnd member variable of the application object. A call to OnFileNew in your override of the InitInstance member function of CWinApp sets m_pMainWnd for you. If you do not call OnFileNew, you must set the variable's value in InitInstance yourself. (SDI COM component (server) applications may not set the variable if /Embedding is on the command line.) Note that m_pMainWnd is now a member of class CWinThread rather than CWinApp.

See also

Document Templates and the Document/View Creation Process
Document Template Creation
Document/View Creation
Creating New Documents, Windows, and Views