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

George Shepherd, David Kruglinski

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

A More Advanced Document-View Interaction

If you're laying the groundwork for a multi-view application, the document-view interaction must be more complex than the simple interaction shown in example Ex15a. The fundamental problem is this: The user edits in view #1, so view #2 (and any other views) must be updated to reflect the changes. Now you need the UpdateAllViews and OnUpdate functions because the document will act as the clearinghouse for all view updates. The development steps are shown here:

    In your derived document class header file (generated by the MFC Application Wizard), declare your document's data members. If you want to, you can make these data members private and you can define member functions to access them or declare the view class as a friend of the document class.

    In your derived view class, use Class View's Properties window to override the OnUpdate virtual member function. The application framework calls this function whenever the document data has changed for any reason. OnUpdate should update the view with the current document data.

    Evaluate all your command messages. Determine whether each one is document-specific or view-specific. (A good example of a document-specific command is the Clear All command on the Edit menu.) Now map the commands to the appropriate classes.

    In your derived view class, allow the appropriate command message handlers to update the document data. Be sure that these message handlers call the CDocument::UpdateAllViews function before they exit. Use the type-safe version of the CView::GetDocument member function to access the view's document.

    In your derived document class, allow the appropriate command message handlers to update the document data. Be sure that these message handlers call the CDocument::UpdateAllViews function before they exit.

The sequence of events for the complex document-view interaction is shown here:

Application starts

CMyDocument object is constructed

CMyView object is constructed

Other view objects are constructed

View windows are created

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

CDocument::OnNewDocument is called

CView::OnInitialUpdate is called

CMyView::OnUpdate is called

The view is initialized

User executes view command

CMyView functions update CMyDocument data members

CDocument::UpdateAllViews is called

OnUpdate functions are called for other views

User executes document command

CMyDocument functions update data members

CDocument::UpdateAllViews is called

CMyView::OnUpdate is called

Other views' OnUpdate functions are

called

User exits application

View objects are destroyed

CMyDocument object is destroyed