Solved

Re-open the open Document

Posted on 1997-11-13
3
411 Views
Last Modified: 2013-11-19
I would like to open, and therefore re-initialize, the MFC document which is currently open. This is a SDI application.
The default behavior is to NOT reload a file (ie. de-serialize a document) if it is the current document. Selecting the file from either the MRU list or the Open Dialog only works when the selected file is not the current open document, weither dirty or not.
Is there some way to tell MFC to allow document re-loading? or extend the framework in some fashion (probably CWinApp or CDocManager)?
0
Comment
Question by:reichj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
wpinto earned 150 total points
ID: 1309907
Hi reichj,

You will need to extend CSingleDocTemplate().

CMySingleDocTemplate : public CSingleDocTemplate
{
  ... construction/destruction
  see ...\mfc\include\afxwin.h for CDocTemplate() source

Now override MatchDocTemplate()
public:
      Confidence MatchDocType(LPCTSTR pszPathName, CDocument*& rpDocMatch);
}

Confidence CMySingleDocTemplate::MatchDocType(...)
{
  Confidence match;
  match = CSingleDocTemplate::MatchDocType(...);

  // This is the main part. Fool the calling fuction into believing that the document is not open
  if (match == CDocTemplate::yesAlreadyOpen)
  match = CDocTemplate::noAttempt;

  return match;
}

Hope this helps

Wilfred

0
 
LVL 2

Expert Comment

by:wpinto
ID: 1309908
Now use CMySingleDocTemplate wherever you would use CSingleDocTemplate
0
 

Author Comment

by:reichj
ID: 1309909
Wilfred,

Thanks for the response.
Unfortunatly the code did'nt work perfectly at first. By the speed of your response I believe the code did work in the past, maybe on an older version of MFC (I'm using VC++ ver 5.0).
It seems CDocManager::OpenDocumentFile() (in docmgr.cpp) needs a little re-writing (I'll leave that up to microsoft). In any event 'match' is not tested, instead 'rpDocMatch' is tested against NULL to see if MatchDocType() has an already open doc. On top of that, setting 'match' to CDocTemplate::noAttempt forces a "Failed to open Doc" messagebox to appear.

According to the documentation :

If the file is not open but the extension in lpszPathName matches the extension specified by CDocTemplate::filterExt, this function returns CDocTemplate::yesAttemptNative and sets rpDocMatch to NULL

Thus, the body of CMySingleDocTemplate::MatchDocType() should be

Confidence MatchDocType(LPCTSTR lpszPathName, CDocument*& rpDocMatch)
{
      Confidence match;
      match = CSingleDocTemplate::MatchDocType(lpszPathName, rpDocMatch);

      // Fool calling function into believing that the
      // Doc is not open.
      if (match == CDocTemplate::yesAlreadyOpen){
            match = CDocTemplate::yesAttemptNative;
            rpDocMatch = NULL;
            POSITION p = GetFirstDocPosition();
            GetNextDoc(p)->SetModifiedFlag(FALSE);
      }
      return (match);
}

I also clear the Modified flag since it doesnt make much sense to get a SaveFile dialog prior to opening the same Doc. Of course just using the first doc position only works for an SDI app.

Thanks,
Jeff Reich


0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 215
bigDiff challenge 17 142
sum13 challenge 24 148
Dell Alienware Graphics Amplifier Driver for non-Alienware systems 18 621
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question