Click here to Skip to main content
15,906,558 members
Articles / Desktop Programming / MFC
Article

An CMultiDocTemplate extension

Rate me:
Please Sign up or sign in to vote.
3.50/5 (3 votes)
1 Sep 2002CPOL 97.7K   1.7K   19   12
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
China China
I love tech!

Comments and Discussions

 
GeneralGood job Pin
Kony Han8-Mar-06 15:18
Kony Han8-Mar-06 15:18 
GeneralRe: Good job Pin
Yi Yang8-Mar-06 16:43
Yi Yang8-Mar-06 16:43 
Generalopen a different documents in MDI application without the first window blank Pin
si197274-Jul-03 5:10
si197274-Jul-03 5:10 
GeneralBug Fixed Pin
Yi Yang1-Sep-02 19:01
Yi Yang1-Sep-02 19:01 
GeneralSimilar article Pin
Warren Stevens29-Aug-02 4:11
Warren Stevens29-Aug-02 4:11 
GeneralRe: Similar article Pin
Yi Yang29-Aug-02 18:19
Yi Yang29-Aug-02 18:19 
GeneralAnother option Pin
Dimitris Vasiliadis28-Aug-02 13:29
Dimitris Vasiliadis28-Aug-02 13:29 
Why let the application create the document since you are going to replace it when opening an existing document. You could prevent your application from creating it on startup. If you want a new document, just select File|New or File|Open, etc...Smile | :)
To do so, you could edit C???App::InitInstance() and after the ParseCommandLine(cmdInfo) make your tests to allow creation of the document template or not.
<br />
BOOL CMyDemoApp::InitInstance()<br />
{<br />
    ...<br />
    // Parse command line for standard shell commands, DDE, file open<br />
    CCommandLineInfo cmdInfo;<br />
    ParseCommandLine(cmdInfo);<br />
<br />
    if (m_nShellCommand == FileNew)<br />
        m_nShellCommand = FileNothing;<br />
    ...<br />
}<br />

GeneralRe: Another option Pin
Yi Yang28-Aug-02 18:06
Yi Yang28-Aug-02 18:06 
GeneralRe: Another option Pin
Dimitris Vasiliadis4-Jul-03 6:10
Dimitris Vasiliadis4-Jul-03 6:10 
GeneralPossible problem Pin
Roger Allen27-Aug-02 22:21
Roger Allen27-Aug-02 22:21 
GeneralRe: Possible problem Pin
Yi Yang28-Aug-02 18:10
Yi Yang28-Aug-02 18:10 
GeneralRe: Possible problem Pin
Yi Yang28-Aug-02 18:21
Yi Yang28-Aug-02 18:21 

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

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