1 / 45

Working with Documents and Views

Working with Documents and Views. Objectives. In this chapter you will learn: About documents and views About document interfaces How to work with the CView class How to work with the CDocument Class How to store data. Documents and Views.

vail
Download Presentation

Working with Documents and Views

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Working with Documents and Views Microsoft Visual C++ .NET Chapter 11

  2. Objectives In this chapter you will learn: • About documents and views • About document interfaces • How to work with the CView class • How to work with the CDocument Class • How to store data Microsoft Visual C++ .NET Chapter 11

  3. Documents and Views • With the exception of dialog-based applications, almost all MFC programs include a window class derived from the CFrameWnd class • The windows displayed by classes that derive from the CFrameWnd class are referred to as frame windows • To refresh your memory on the CFrameWnd class, Figure 11-4 shows a basic MFC program that includes an application class and a window class derived from the CFrameWnd class Microsoft Visual C++ .NET Chapter 11

  4. Documents and Views • Figure 11-5 shows the frame window displayed by the basic MFC application • A document-based application allows users to read and write to documents that are associated with the application Microsoft Visual C++ .NET Chapter 11

  5. Documents and Views • The MFC document/view architecture separates a program’s data from the way that data is displayed and accessed by users • A program’s data (the document) is managed and stored by a class derived from the CDocument class Microsoft Visual C++ .NET Chapter 11

  6. Documents and Views • How a document-based program’s data is displayed to the user is controlled by one or more classes derived from the CView class Microsoft Visual C++ .NET Chapter 11

  7. Documents and Views • Figure 11-7 illustrates the concept of multiple CView classes that display and manipulate the data contained in a single CDocument class Microsoft Visual C++ .NET Chapter 11

  8. Documents and Views • As an example of document/view architecture, consider Microsoft Excel, which is a typical Windows spreadsheet application that employs the document/view architecture • Figure 11-8 shows an example of a simple Excel spreadsheet containing several charts that graphically display portions of the spreadsheet data Microsoft Visual C++ .NET Chapter 11

  9. Documents and Views • You are not required to use document/view architecture in your document-based MFC programs; you can actually turn it off when you use the MFC Application Wizard to build a document-based application • However, by enabling document/view architecture, your document-based MFC applications automatically inherit a wide range of document functionality that allows you to perform all kinds of document-specific tasks Microsoft Visual C++ .NET Chapter 11

  10. Document Interfaces • The single document interface, or SDI, allows users to have only one document open at a time • The multiple document interface, or MDI, allows users to have multiple documents open at the same time • Figure 11-9 shows an example of the single document that is available in Notepad Microsoft Visual C++ .NET Chapter 11

  11. Document Interfaces • Figure 11-10 shows an example of Word with multiple documents open Microsoft Visual C++ .NET Chapter 11

  12. Word Multiple Document Interface Microsoft Visual C++ .NET Chapter 11

  13. The CView Class • You display a window created from a class derived from CView within a frame window • This means the CView window is a child of a frame window • The CView window completely covers the frame window’s client area, but does not cover visual interface elements such as the title bar or scroll bars • Figure 11-11 illustrates this concept Microsoft Visual C++ .NET Chapter 11

  14. Frame Window and View Window Microsoft Visual C++ .NET Chapter 11

  15. CView Child Classes • The classes that derive from CView are used for creating more specialized view windows Microsoft Visual C++ .NET Chapter 11

  16. CView Child Classes • Start creating a single text editor SDI application based on the CEditView class using the directions shown on pages 589 through 594 in the textbook Microsoft Visual C++ .NET Chapter 11

  17. CView Child Classes Microsoft Visual C++ .NET Chapter 11

  18. Text Box Descriptions in the Document Template Strings Tab of the MFC Application Wizard Microsoft Visual C++ .NET Chapter 11

  19. CView Child Classes Microsoft Visual C++ .NET Chapter 11

  20. CView Child Classes Microsoft Visual C++ .NET Chapter 11

  21. CView Child Classes • The PreCreateWindow() function is called by the MFC framework before a frame window is created in order to specify the window’s styles Microsoft Visual C++ .NET Chapter 11

  22. CView Child Classes • To build the Simple Text Editor program and examine its functionality, use the instructions outlined on pages 595 and 596 of the textbook Microsoft Visual C++ .NET Chapter 11

  23. CView Child Classes • To start creating the Invoice program, use the steps shown on page 597 of the textbook • Figure 11-24 shows an example of how the controls in the window should be placed Microsoft Visual C++ .NET Chapter 11

  24. CView Child Classes • To add controls to the Invoice program’s view window along with DDX data members for each control. perform the procedures illustrated on pages 598 through 600 of the textbook Microsoft Visual C++ .NET Chapter 11

  25. CView Child Classes Microsoft Visual C++ .NET Chapter 11

  26. DDX Data Member Names and Types Microsoft Visual C++ .NET Chapter 11

  27. CView Member Functions • A view class is responsible for graphically displaying a document’s data and for handling the manipulation of that data according to user requests • The CView class contains various functions for displaying and manipulating data • The primary CView class member functions that you use to display and manipulate data are as follows: • OnDraw() - GetDocument() -OnUpdate() Microsoft Visual C++ .NET Chapter 11

  28. The GetDocument() Function • The GetDocument() function returns a pointer to the document associated with a view • If you examine the header file for the CInvoiceView class, you will see that the GetDocument() function is declared in the public section and returns a pointer data type of the document class, CInvoiceDoc • The GetDocument() function returns a pointer to the m_pDocument data member that you can use anywhere in the view class when you need to access the document’s data Microsoft Visual C++ .NET Chapter 11

  29. The GetDocument() Function • The release build version of the GetDocument() function is defined as an inline function in the view class interface file, within a pair of #ifndef and #endif preprocessor directives immediately following the class definition • To add message map functions to the CInvoiceView class along with an updateDataMembers() function, use the steps listed on pages 602 through 604 of the textbook Microsoft Visual C++ .NET Chapter 11

  30. The OnUpdate() Function • Each view class inherits an OnUpdate() function that is called each time the document class changes or whenever the document class executes an UpdateAllViews() function • The OnUpdate() function allows all of the view windows in an application to display the most current data • The UpdateAllViews() function is a member function of CDocument and causes each view window’s OnUpdate() function to execute in order to allow each view to display the most recent data Microsoft Visual C++ .NET Chapter 11

  31. The OnUpdate() Function • Passing a single value of NULL to the UpdateAllViews() function informs the system that all views in the program should be updated • Note that you execute the UpdateAllViews() function from a derived CDocument class, not a derived CView class • The inherited default OnUpdate() function does not automatically update its view • To add an OnUpdate() function to the Invoice program’s CView class, perform the processes listed on pages 605 and 606 of the textbook Microsoft Visual C++ .NET Chapter 11

  32. The CDocument Class • Once the MFC Application Wizard creates your derived document class, you must perform the following tasks: • Create data members to temporarily hold the data for the current document • Override the CDocument class’s member functions to customize the creating, loading, and saving mechanisms of the document/view architecture • Override CDocument’s Serialize() member function in order to read the document’s data from and write the document’s data to a file Microsoft Visual C++ .NET Chapter 11

  33. CDocument Data Members • You create data members in the CDocument class by adding declarations for each data member to the interface file, the same way you create data members in other classes • You can then initialize each data member in the class constructor and use appropriate set and get member functions to manipulate each data member • One of the main differences between CDocument and other types of classes is that you call the set and get member functions from the view class using the GetDocument() function Microsoft Visual C++ .NET Chapter 11

  34. CDocument Member Functions • Although many of the CDocument member functions are called automatically by the MFC framework, you can override each of the functions to customize how your application creates, loads, and saves documents • Figure 11-27 lists some of the functions that are inherited from the CDocument class • The function names in Figure 11-27 that are preceded by On are called automatically by the MFC framework Microsoft Visual C++ .NET Chapter 11

  35. Common CDocument Member Functions Microsoft Visual C++ .NET Chapter 11

  36. CDocument Member Functions • The only functions for which the MFC Application Wizard automatically provides overridden implementations are the OnNewDocument() and Serialize() functions, because the OnNewDocument() function is one of the most commonly overridden functions and the Serialize() function is necessary for reading data from, and writing data to, files • Figure 11-28 in the text shows the Invoice program’s derived CDocument class, InvoiceDoc.h • Notice the overridden OnNewDocument() and Serialize() functions Microsoft Visual C++ .NET Chapter 11

  37. CDocument Member Functions • When a user creates a new document in an SDI application, MFC reuses the same document object • Because the program reuses the same document object, the values contained in the document class’s data members will appear in the fields, unless you reinitialize the data members using the DeleteContents() function • To override the DeleteContents() function in the CInvoiceDoc class, refer to the procedures on pages 610 and 611 of the textbook Microsoft Visual C++ .NET Chapter 11

  38. CDocument Member Functions • The SetModifiedFlag() function is used by the MFC framework to determine if a document has been modified since the last time it was saved • When you change a document’s data, you pass to the SetModifiedFlag() function a value of TRUE to indicate that the document needs to be saved Microsoft Visual C++ .NET Chapter 11

  39. Storing Data • Persistence refers to the ability for data to continue to exist after the program closes • The process of storing data to, and retrieving data from, a persistent storage medium is called serialization • The process of retrieving data stored in a disk file is sometimes referred to as deserialization Microsoft Visual C++ .NET Chapter 11

  40. Storing Data • In addition to deriving from the CObject class, basic serialized classes must also include the following: • The DECLARE_SERIAL() macro • An empty, default constructor • An overridden Serialize() function • The Serialize() function reads data from and writes data to a disk file • The IMPLEMENT_SERIAL macro defines the code and various functions needed by a serialized class Microsoft Visual C++ .NET Chapter 11

  41. Storing Data • The CArchive class is used for writing data to and reading data from a storage medium • To actually write to and read from a disk file, you use the CArchive object within the if and else structure’s command blocks, along with the insertion operator (<<) and the extraction operator (>>) and the document class’s data members Microsoft Visual C++ .NET Chapter 11

  42. Storing Data • Recall that you used the insertion and extraction operators with console applications • However, instead of writing to the output stream and reading from the input stream, when used with serialization, the insertion and extraction operators write to and read from a CArchive object • Modify the Invoice program’s Serialize() function so the program can save documents to disk by following the instructions outlined on pages 615 and 616 of the textbook Microsoft Visual C++ .NET Chapter 11

  43. Summary • A document is a file that is associated with a particular application and contains different sets of data, depending on your program’s functionality and each user’s needs • The single document interface, or SDI, allows users to have only one document open at a time • The multiple document interface, or MDI, allows users to have multiple documents open at the same time Microsoft Visual C++ .NET Chapter 11

  44. Summary • Each view class inherits an OnUpdate() function that is called each time the document class changes or whenever the document class executes an UpdateAllViews() function • The UpdateAllViews() function is a member function of CDocument and causes each view window’s OnUpdate() function to execute in order to allow each view to display the most recent data Microsoft Visual C++ .NET Chapter 11

  45. Summary • When a user creates a new document in an SDI application, MFC reuses the same document object • You need to override the DeleteContents() function to reinitialize the document object’s data members each time a new document is created • The process of storing data to, and retrieving data from, a particular storage medium is called serialization Microsoft Visual C++ .NET Chapter 11

More Related