Programming with Microsoft Visual C++.NET 6ed [Electronic resources]

George Shepherd, David Kruglinski

نسخه متنی -صفحه : 319/ 137
نمايش فراداده

The Simplest Document-View Application

Suppose you don't need multiple views of your document, but you plan to take advantage of the application framework's file support. In this case, you can forget about the UpdateAllViews and OnUpdate functions. Simply follow these steps when you develop the application:

  1. In your derived document class header file (generated by the MFC Application Wizard), declare your document's data members. These data members are the primary data storage for your application. You can make these data members public, or you can declare the derived view class a friend of the document class.

  2. In your derived view class, override the OnInitialUpdate virtual member function. The application framework calls this function after the document data has been initialized or read from disk. ( Chapter 16 discusses disk file I/O.) OnInitialUpdate should update the view to reflect the current document data.

  3. In your derived view class, let your window message handlers, command message handlers, and your OnDraw function read and update the document data members directly, using GetDocument to access the document object.

The sequence of events for this simplified document-view environment is as follows:

Application starts

CMyDocument object is constructed

CMyView object is constructed

View window is created

CMyView::OnCreate is called (if it is mapped)

CMyDocument::OnNewDocument is called

CMyView::OnInitialUpdate is called

View object is initialized

View window is invalidated

CMyView::OnDraw is called

User edits data

CMyView functions update CMyDocument data members

User exits application

CMyView object is destroyed

CMyDocument object is destroyed