I am using a standard SDI application as generated by the application
wizard. When re-opening the same document (i.e. loading it a second time)
ID_FILE_OPEN calls CWinApp::OnFileOpen as expected, but *not*
CDocument::OnOpenDocument. When I load another file, both functions are
called.
Why does the framework first let me choose a file, but afterwards does not
reload the document just because the filepath did not change?
Any help is appreciated,
Daniel
Dr. Daniel Krüger <dan...@meteo-graphics.de> wrote in message
news:8f91p8$q1d$1...@unlisys.unlisys.net...
below you can find Roberts solution to the problem:
Daniel
###################################
The following is happening in CDocManager::OpenDocumentFile..
if (match == CDocTemplate::yesAlreadyOpen)
break; // stop here
You will be able to override your CWinApp::OnFileNew with
CYourApp::OnFileNew perhaps then you might like to try something different
without much change at all
CYourApp::OnFileNew()
{
m_pTheSDITemplate->OpenDocumentFile(...); // whatever
// instead of letting the framework call CDocManager::OnFileNew
}
Don't forget to change the message map for your application class and
register the document template with a variable that hangs around
eg.
m_pTheSDIDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CSDITryDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CSDITryView));
AddDocTemplate(pDocTemplate);
where m_pTheSDIDocTemplate is declared in your application class
CSingleDocTemplate *m_pTheSDIDocTemplate;
The skill is in diverging the code just enough from the framework ( and no
more ) to get your application to do what you want
PRB: MFC Does Not Reopen an Open Document
----------------------------------------------------------------------------
----
The information in this article applies to:
The Microsoft Foundation Classes (MFC), used with:
Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52
Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, 2.2, 4.0
----------------------------------------------------------------------------
----
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.
MORE INFORMATION
Sample Code
/* Compile options needed: none
*/
void CWinApp::OnFileOpen()
{
// prompt the user (with all document templates)
CString newName;
if (!DoPromptFileName(newName, AFX_IDS_OPENFILE,
OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, TRUE, NULL))
return; // open cancelled
// Process newName string if necessary
// Get pTemplate, a pointer to one of the app's document templates
pTemplate->OpenDocumentFile(newName);
} // end of CWinApp::OnFileOpen()
Š Microsoft Corporation 1999, All Rights Reserved.
Contributions by Jason Strayer, Microsoft Corporation
Additional query words: 1.00 1.50 2.00 2.10 2.20 4.00 2.50 2.51 2.52 3.00
3.10 3.20
Keywords : kbDocView kbFileIO kbMFC kbVC100 kbVC150 kbVC200 kbVC400
kbGrpMFCATL
Version : winnt:
Platform : winnt
Issue type : kbprb
Technology : kbvc
Last Reviewed: March 13, 2000
Š 2000 Microsoft Corporation. All rights reserved. Terms of Use.