Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


When a new file has been created, it displays as empty. Such a document is referred to as
"clean".

We also saw earlier that, to help the user open an existing document, you can create a
menu item identified as ID_FILE_OPEN and associate it with the
CWinApp::OnFileOpen() method in your InitInstance() method. This time also, the menu
item only provides a convenient way to perform the action. It makes the document
available to the application and not to the document. Once a user has initiated the action
of opening an existing file, you may want to check that the document not only exists but
also can be opened and make its contents available to the user. This job can be handled
by the OnOpenDocument() virtual method of the CDocument class. Its syntax is:

virtual BOOL OnOpenDocument(LPCTSTR lpszPathName);

This method usually results from the user clicking File -> Open... on the main menu,
communicating a desire to open a file. When this action is initiated, the
OnOpenDocument() method retrieves the path of the file as the lpszPathName argument.
If the path is valid, that is, if the file exists, you can then check it or perform a last minute
task before the file is opened. For example you can use this method to decide how the
contents of the file will be displayed or dealt with by the document. You can also use to
prevent the user from opening any file or to prevent the user from opening any file at all.
You can also use this method to allow the user to open a type of file that your
CDocument-derived class would not expect.

If the user has opened an existing file but has not (yet) changed anything in the
document, the file is also called "clean". As we will learn eventually, some files can be
changed and some do not allow modification. If a document allows the user to change it,
he or she can manipulate it as necessary, including adding, deleting, or moving items.
Once a user has changed anything on the document, the file is referred to as "dirty". You
may want to keep track of such change(s) so you would know eventually if the document
needs to be saved. To help you with this, the CDocument class provides the
SetModifiedFlag() method. Its syntax is:

void SetModifiedFlag(BOOL bModified = TRUE);

To mark a document as clean or dirty, call the SetModifiedFlag() method. If you pass the
bModified argument as TRUE, the document has been changed. Since the TRUE
constant is its default value, you can also call the method simply as SetModifiedFlag().
To specify that the document is clean, pass the argument as FALSE. You can call this
method whenever you judge necessary. For example, if the user saves the document
while working on it but makes another change, you can mark it clean when it has just
been saved and mark it dirty if the user changes anything again. At any time, you can
check whether the document is dirty or clean using the CDocument::IsModified()
method. Its syntax is:

BOOL IsModified();

This method simply checks the document to find out if it has been modified since the last
time it was accessed. If the document has been modified, this method would return
TRUE. If the document is clean, it returns FALSE.

Another action the user can perform on a document is to send it electronically to an email
recipient. To allow the user to send the current document as an email attachment, first an
email client (such as MS Outlook) must be installed on the user's computer. Therefore,
add a menu item IDentified as ID_FILE_SEND_MAIL. Then, in the message table of
your document implementation, add the following two macros:
Free download pdf