Updating Multiple Views
To illustrate some issues about updating multiple views, try the following:
Run Scribble (your Step 3 version) and draw a few strokes in the open document.
From the Window menu, click the New Window command.
This opens a new document window displaying the same drawing. The document object now has two view objects connected to it.
Click the Tile command so you can see both views at the same time.
Add some more new strokes in the first window.
Do the new strokes appear in the other window simultaneously? No.
Why is this the case? Right now, Scribble has no way of telling each open document window what is happening in any other open document window. (This is illustrated in the figure below.) You could force a repaint — for instance, by minimizing and then restoring the window. Then its OnDraw
function would display the drawing again, including the new strokes. But how can you ensure that all the views attached to a document reflect changes to the document as soon as they are made?
Multiple Views on a Document Without Updating
Each view must notify the other views whenever it has modified the document. MFC provides a standard mechanism for notifying views of modifications to a document through the UpdateAllViews member function of the CDocument class.
The UpdateAllViews function traverses the list of views attached to the document. For each view in the list, the function calls the OnUpdate member function of the CView class. The OnUpdate function is where the view responds to changes in the document; the default implementation of the function invalidates the client area of the view, causing it to be repainted. The simplest way for you to use this updating mechanism in your application is to call the document’s UpdateAllViews function whenever a view modifies a document in response to a user action.
You can also perform more efficient repainting with this updating mechanism if you use the parameters of the UpdateAllViews function. Here is the declaration of UpdateAllViews:
void UpdateAllViews(CView* pSender, LPARAM lHint = 0L,
CObject* pHint = NULL);
The first argument identifies the view that made the modifications to the document. This is specified to keep the UpdateAllViews function from performing a redundant notification. The view that made the modifications doesn’t need to be told about them. The second two arguments are “hints.” You can use these hints to describe the modifications that the view made.
The UpdateAllViews function gives the hints to every view attached to the document by passing them as parameters to the OnUpdate member function. You can override OnUpdate to interpret those hints and update only the area of the display that corresponds to the modified portion of the document. Thus, if another view is displaying a completely different portion of the document, it doesn’t have to perform any repainting at all.