|
Chapter 4: Implementing View Classes
This chapter examines the different types of view classes available in the Microsoft Foundation Class (MFC) Library. The chapter begins with a general discussion of how views fit into the document/view architecture, and continues with sections on some of the views. The CHTMLView class, which provides Internet browsing functionality, is not covered in this chapter. For information about CHTMLView, see "Implementing HTML Views" on page 469 in Chapter 9, "Building Internet Applications."
ObjectivesAfter completing this chapter, you will be able to:
Introduction to ViewsThe view class, CView, physically represents the client area of the application. Logically, CView represents a viewport of the information contained in the document class. It also allows user input through the mouse or keyboard.MFC supports a wide variety of views. The following illustration shows the hierarchy of MFC view classes.
Figure 4-1. By deriving your application’s view from one of these classes, the view inherits support for interaction with the application framework. This section provides information on the CView class and how it fits into document/view architecture. It also introduces the relationship between the view and the document. The CView class simplifies the implementation of printing and print preview for your documents. For more information about implementing printing features, see Chapter 10, "Printing and Print Preview."
The CView ClassThe CView class provides the basic functionality for user-defined view classes.A view is attached to a document and acts as an intermediary between the document and the user. The view renders an image of the document on the screen or printer and interprets user input as operations on the document. A view is a child of a frame window. More than one view can share a frame window, as in the case of a splitter window. When the user opens a new window or splits an existing one, the framework constructs a new view and attaches it to the document.
Relationship Between a View and a DocumentA view can be attached to only one document, but a document can have multiple views attached to it at once, as when the contents of the document are displayed in a splitter window or in multiple child windows in an MDI application.A view displays a document’s data. The document provides the view with the necessary details about its data. You can have the view access the document’s data members directly, or you can provide member functions in the document class for the view class to call.
Coordinating the Views of a DocumentTo manage the complex process of creating documents with their associated views and frame windows, the framework uses two document template classes:
You use CSingleDocTemplate with single-document interface (SDI) applications to create and store one document of one type. You use CMultipleDocTemplate with multiple-document interface (MDI) applications to create and store multiple documents of one type. When a document’s data changes, the view responsible for the changes typically calls the CDocument::UpdateAllViews function for the document, which notifies all the other views by calling the OnUpdate member function for each view. The default implementation of OnUpdate invalidates the view’s entire client area. You can override this function to invalidate only those parts of the client area that you want to redraw.
Managing Document/View InteractionTo manage the interaction between a document and its view, you must complete the following steps.
Establishing CommunicationTo establish communication between the current document and a view, create a pointer to the appropriate type of document and call the view’s GetDocument member function, as shown in the following example code:
// Samples\CH04\Mdiapp Displaying DataTo display data in a view, override the OnDraw function. The sample code on the following page overrides the OnDraw function. To copy this code for use in your own projects, see "Overriding OnDraw" on the accompanying CD-ROM.
// Overriding the OnDraw function to display data in a view Updating the ViewTo update the view(s) when data in the document changes, use either one of these procedures:
Use in applications that use a single view. Use in applications that use multiple views. In applications that use views of different types, calling the UpdateAllViews function may not be the easiest solution for updating data. For information about a method that bypasses the UpdateAllViews function, see "Coordinating Multiple Interrelated Views" on page 183 in this chapter.
TIP: You can use the pHint and lHint parameters of UpdateAllViews to pass information to the views about the modifications made to the document. You can encode information by using lHint, and you can define a CObject-derived class to store information about the modifications and pass an object of that class by using pHint. Override the CView::OnUpdate member function in your CView-derived class to optimize the updating of the view’s display, based on the information passed. For more information about the UpdateAllViews function, search for "UpdateAllViews" in MSDN Library Visual Studio 6.0. The following sample code shows how an MDI application uses the UpdateAllViews function to ensure that all views associated with the current document are correctly repainted when the user changes the color of the text displayed in the view. To copy this code for use in your own projects, see "Updating the Views in an MDI Application" on the accompanying CD-ROM.
// Repainting all views of a document when the user changes the textFor more information about functions used to update views, search for "Updating Views" in MSDN Library Visual Studio 6.0.
Adding Multiple ViewsIt is possible to support more than one view of a single document. To help you implement multiple views, a document object keeps a list of its views, provides member functions for adding and removing views, and supplies the UpdateAllViews member function, which sends a message to the views when the user changes the data in a document.This section explains how to create multiple views of the same document for SDI and MDI applications. In the sample applications for this section, multiple views share a single frame window. The views are constructed from different classes, each view providing a different way to display the same document. For example, one view might show the text in a document in a normal mode, while the other view shows the text in italic.
Adding Multiple Views to an SDI ApplicationIn an SDI application, you can have multiple views of the same document. The views are constructed from different classes, each view providing a different way to display the same document. Several steps are required to add a second view to an SDI application.To create an SDI application with two views of the same document
TIP: In an application with only one view class, place command handlers in the view class. In an application with more than one view class, place command handlers in the CMainFrame (SDI) or CChildFrame (MDI) class. The functions CFrameWnd::GetActiveView and CFrameWnd::GetActiveDocument facilitate accessing the view of the document from your main frame or child frame. Use the GetDocument functions from your existing view class as a model. ClassWizard provides the new view class with a protected constructor. Make sure the constructor differs from the existing constructor in a way other than scope. The protected destructor is not necessary, and the new constructor you have added is already public. These variables will be pointers to the two view objects your application uses. Initialize these pointers to NULL (0) in the constructor of CMainFrame, as shown in the following example code: class CMainFrame : public CFrameWnd The View menu is a good place to insert the menu selections, but you can choose any of the drop-down menus. The following sample code shows how to add the command handler to switch to the new view. To copy this code for use in your own projects, see "Command Handler to Switch to New View" on the accompanying CD-ROM. // To switch to the new view and hide the default view The following sample code shows how to add the command handler that restores the default view. To copy this code for use in your own projects, see "Command Handler to Restore Default View" on the accompanying CD-ROM. // To switch to the default view and hide the second view (the italics You can choose to leave the inactive view in the application, or you can remove the inactive view from your application. The previous sample code uses CDocument::RemoveView to remove the inactive view. Removing it means that when the document object is changed and CDocument::UpdateAllViews is called, the inactive view object’s OnDraw function will not be called. Removing the inactive view is not required, but it makes the application more efficient. If the inactive view is not removed from the document object’s list of attached views, nothing detrimental will happen when the inactive view’s OnDraw function executes. In an application with only two views, each with simple OnDraw functions, you will not be able to tell much difference. void CMainFrame::OnUpdateViewDefault(CCmdUI* pCmdUI) The code samples discussed in this topic appear in the sample application SDI2VIEWS, which is located in the folder <install folder>\Samples\Ch04.
Microsoft® Mastering: MFC Development Using Microsoft Visual C++® 6.0
Last Updated: Friday, July 6, 2001 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||