PRB: MFC Does Not Reopen an Open Document (139828)
The information in this article applies to:
- The Microsoft Foundation Classes (MFC), when used with:
- Microsoft Visual C++ 1.51
- Microsoft Visual C++ 1.52
- Microsoft Visual C++, 32-bit Learning Edition 4.0
- Microsoft Visual C++, 32-bit Professional Edition 2.0
- Microsoft Visual C++, 32-bit Professional Edition 2.1
- Microsoft Visual C++, 32-bit Professional Edition 2.2
- Microsoft Visual C++, 32-bit Professional Edition 4.0
This article was previously published under Q139828 SYMPTOMS
In a default MFC AppWizard application, the framework will not reopen a
document file from disk that is currently open in the application.
RESOLUTION
This is by design. In a typical MFC application, the Open File command is
mapped to the CWinApp::OnFileOpen() function. In earlier versions of MFC,
this function in turn called the CWinApp::OpenDocumentFile() function.
Since MFC 4.0, there is now an intervening CDocManager class, but the call
to OnFileOpen() still eventually results in a call to
CWinApp::OpenDocumentFile().
CWinApp::OpenDocumentFile() first processes the string holding the
requested file name. Then it searches through the list of document
templates that were added for the application by calls to AddDocTemplate in
order to find the best match between the name of the file and a document
template to open it with. At this point, if OpenDocumentFile() finds that
this file is already currently opened for one of the templates,
OpenDocumentFile() activates the view for that file and then returns. It
does not re-open the file.
If that document file is not currently open and OpenDocumentFile() has
found a valid template to open the file with, it calls that template's
OpenDocumentFile() function. This function is responsible for opening the
file and loading its data into an appropriate document.
In some situations, you may want to reopen an open document. For example,
Notepad does this. On a file open request, Notepad first displays a prompt
dialog to allow the user to save a modified file. If the user does not
click cancel on this dialog box, Notepad then brings up the File Open
dialog box. If the user chooses to reopen the current file, Notepad rereads
it from disk and discards any unsaved changes.
To duplicate this behavior in an MFC program, the programmer needs to
override the OpenDocumentFile() member function of CWinApp. Or, if the
appropriate template for the file is easy to determine (such as when the
application has only one kind of doc template), it would be possible to
call the template's OpenDocumentFile() directly from an override of
CWinApp::OnFileOpen(). This is demonstrated in the "Sample Code" section of
this article.
Note that MFC will display the Save Modified prompt dialog after the Open
File dialog box; this is counter to the behavior of Notepad.
STATUS
This behavior is by design.
Modification Type: | Major | Last Reviewed: | 12/2/2003 |
---|
Keywords: | kbDocView kbFileIO kbprb KB139828 |
---|
|