Click here to Skip to main content
Licence 
First Posted 26 Aug 2002
Views 76,451
Bookmarked 18 times

An CMultiDocTemplate extension

By | 1 Sep 2002 | Article
A more real Document Template Class

Introduction

When we build and run an MFC MDI application, it initially opens a new empty document for us. Then when we open an existing document, it creates a new frame and opens the document in that frame. But I feel that it will be more kindly if it just replaces the unmodified empty document and open the existing document in the same frame. So I decided to derive a new class from CMultiDocTemplate and name it CMultiDocTemplateEx to do such things.

I just overrode OpenDocumentFile(LPCTSTR lpszPathName, BOOL bMakeVisible), before directly calling the base class's OpenDocumentFile() method. I made some test. If there is only ONE document and it is UNMODIFIED, I call the document's OnOpenDocument() with lpszPathName, and call it's SetPathName(). Any other case, we call CMultiDocTemplate::OpenDocumentFile() directly. The code is quite straightforward.

CDocument* CMultiDocTemplateEx::OpenDocumentFile(LPCTSTR lpszPathName,
                                                    BOOL bMakeVisible)
{
    if (m_docList.GetCount() == 1)
    {
        CDocument* pDocument = (CDocument*)m_docList.GetHead();
        if (pDocument->GetPathName().IsEmpty() && !pDocument->IsModified())
        {
            CWaitCursor wait;
            if (!pDocument->OnOpenDocument(lpszPathName))
            {
                TRACE0("CDocument::OnOpenDocument returned FALSE.\n");
                return NULL;
            }
            pDocument->SetPathName(lpszPathName);
            POSITION pos = pDocument->GetFirstViewPosition();
            CView* pView = pDocument->GetNextView(pos);
            CFrameWnd* pFrame = pView->GetParentFrame();
            InitialUpdateFrame(pFrame, pDocument);
            return pDocument;
        }
    }
    return CMultiDocTemplate::OpenDocumentFile(lpszPathName, bMakeVisible);
}

Do not know how to use it? Okay, add MultiDocTemplateEx.cpp and MultiDocTemplateEx.h to your project and include the header in your stdafx.h like this

#include "MultiDocTemplateEx.h"

Then in the InitInstance, change CMultiDocTemplate to CMultiDocTemplateEx, or just add "Ex" after that word, build, run, test it!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Yi Yang

Web Developer

China China

Member

I love tech!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGood job PinmemberKony Han15:18 8 Mar '06  
GeneralRe: Good job PinmemberYi Yang16:43 8 Mar '06  
Generalopen a different documents in MDI application without the first window blank Pinmembersi197275:10 4 Jul '03  
GeneralBug Fixed PinmemberYi Yang19:01 1 Sep '02  
GeneralSimilar article PinmemberWarren Stevens4:11 29 Aug '02  
GeneralRe: Similar article PinmemberYi Yang18:19 29 Aug '02  
GeneralAnother option PinmemberDimitris Vassiliades13:29 28 Aug '02  
GeneralRe: Another option PinmemberYi Yang18:06 28 Aug '02  
GeneralRe: Another option PinmemberDimitris Vassiliades6:10 4 Jul '03  
GeneralPossible problem PinmemberRoger Allen22:21 27 Aug '02  
GeneralRe: Possible problem PinmemberYi Yang18:10 28 Aug '02  
GeneralRe: Possible problem PinmemberYi Yang18:21 28 Aug '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 2 Sep 2002
Article Copyright 2002 by Yi Yang
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid