Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


BEGIN_MESSAGE_MAP(CTestDoc, CDocument)
ON_COMMAND(ID_FILE_SEND_MAIL, OnFileSendMail)
ON_UPDATE_COMMAND_UI(ID_FILE_SEND_MAIL, OnUpdateFileSendMail)
END_MESSAGE_MAP

Once a user has finished using a document, he or she would need to close it, which is
done by either clicking the System Close button or using the main menu (File -> Exit). If
the user clicks the System Close button , the application would need to close the frame.
At this time, the document would call the OnCloseDocument() method. Its syntax is:

virtual void OnCloseDocument();

You can use this method (which really behave as an event) to decide what to do before a
frame is closed.

When the user decides to close an application, the document class checks the file to know
whether the file is "dirty". If the file is dirty, you may want to ask the user to save or not
save the document. As we saw earlier with the ID_APP_EXIT pre-configured menu, the
framework can check this setting for you. Also, while using a file, the user may want to
save it. If the user is working on a document that was opened from a drive, the document
may be saved immediately behind the scenes. If the user is working on a brand new
document and decides to save it, you may want to check first if this is possible and what
needs to be done in order to save the document.

To help the user save a document, you can create a menu item. Using the
Document/View architecture, add a menu item with the identifier ID_FILE_SAVE in the
IDR_MAINFRAME common resource name. It is that simple. This menu is usually
positioned under File with a caption of &Save.

If the user wants to save a document with a different name and/or a different location,
which is usually done by clicking File -> Save As... from the main menu, create a menu
item with the ID_FILE_SAVE_AS identifier. This menu item is usually placed under
Save in the File main menu. If the user is working on a new document (that has not been
saved previously), or if the user working on a document that is marked as Read-Only, or
if the user decides to close the application after working on a new document, and if the
user decides to save the document, the action would initiate the File -> Save As action.

When the user has decided to save a document, if the document is dirty, a message box
would display to the user for a decision. If the user decides to save the document, the
CDocument class provides the OnSaveDocument() method to validate the action. The
syntax of this method is:

virtual BOOL OnSaveDocument(LPCTSTR lpszPathName);
To save a document, the user must specify where the document would reside. This is
communicated as the lpszPathName argument). In reality, as its name suggests, this
method is not used to save a document. It allows you to validate the action or desire to
save a document. For example, if the user clicks File -> Save on the main menu or if the
user is attempting to close a dirty document, you can use this method to check what is
going or to deny saving the file.

Serialization is the ability to store data on a drive. Therefore, to actually save a file, the
CObject class provides the Serialize() method to its children. To store data of the file, its
information is held by a class called CArchive. When saving a file, a CArchive object is
passed to the Serialize method as reference, which modifies its value.
Free download pdf