Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 5: The Document/View Architecture


CArchive is used to either save the current document or open an existing one. To take
care of each, it uses two methods (ReadObject() and WriteObject()). These methods are
actually implemented using the extraction operators (>> and <<). Whenever you need to
perform serialization in an application, add a method called Serialize() to your document
class and pass it a CArchive object reference. The syntax of this method is:

virtual void Serialize(CArchive& ar);

The implementation of this method may depend on the document.

Practical Learning: Improving the Document



  1. In the Resource View, add the following menu items under the File menu:


Caption ID Prompt
&New\tCtrl+N ID_FILE_NEW Create a new document
&Open...\tCtrl+O ID_FILE_OPEN Open an existing document
&Save\tCtrl+S ID_FILE_SAVE Save the current document
Save &As... ID_FILE_SAVE_AS Save the current document with
a new name or location


  • P&rint Setup... ID_FILE_PRINT_SETUP Change the printer and printing
    options


  • Recent file ID_FILE_MRU_FILE1 Open this file




  • E&xit ID_APP_EXIT Quit the application; prompt the
    save document





  1. To use a CDocument method, in the header file, add the OnNewDocument() function
    to the document as follows:


class CExerciseDoc : public CDocument
{
DECLARE_DYNCREATE(CExerciseDoc)

virtual BOOL OnNewDocument();
DECLARE_MESSAGE_MAP()
};


  1. In the source file, implement the method as follows:

Free download pdf