Click here to Skip to main content
12,951,983 members (58,282 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 23 Feb 2003

Single View in MultiDoc-Application

, 24 Feb 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
Single View in MultiDoc-Application

Single View in MultiDoc-Application

Sometimes it is useful, to have only one single instance of a document view in a multi-doc application.

The procedure for achieving this is quite straight forwarded.  Add a variable of type CMultiDocTemplate* to your application class. Call it m_pTplteOnlyOneView, or give it any name that suits you.  Create a class to view that document type. Call it CViewOnlyOneView, or give it any name that suits you.  In your initializing code of your application (usually InitInstance) add

m_pTplteOnlyOneView = new CMultiDocTemplate(
              IDR_MAIN_MENU, // use your menu-ID
              RUNTIME_CLASS(CMultiDocAppForm), // use your main frame document class
              RUNTIME_CLASS(CChildFrame),      // use your custom MDI child frame
              RUNTIME_CLASS(CViewOnlyOneView));// use your custom document view

With the application wizard of Visual Studio add a menu command handler for opening this document. Be sure to add that handler in your application class. Also add a handler for the update command function.

Place following code inside the update command handler. Be sure that you use your variable names and not mine.

void CMultiDocApp::OnUpdateOnlyOneView(CCmdUI* pCmdUI)
{ pCmdUI->Enable(TRUE);
  if( NULL == m_pTplteOnlyOneView ) pCmdUI->Enable(FALSE);

Place following code inside the command handler. Be sure that you use your variable names and not mine.

void CMultiDocApp::OnOnlyOneView()
{ ASSERT_VALID(m_pTplteOnlyOneView);
  // There may be only one View of that doc type!
  // Is the template initialized?
  POSITION Pos = m_pTplteOnlyOneView->GetFirstDocPosition();
  if( NULL == Pos ) // No
    { m_pTplteOnlyOneView->OpenDocumentFile(NULL);// Initialize
  // Is there an entry for that document type?
  CDocument *pDoc = m_pTplteOnlyOneView->GetNextDoc(Pos);
  if( NULL == pDoc )// No
    { m_pTplteOnlyOneView->OpenDocumentFile(NULL);// Initialize
  // Is the View for that document type initialized?
  Pos = pDoc->GetFirstViewPosition();
  if( NULL == Pos )// No
    { m_pTplteOnlyOneView->OpenDocumentFile(NULL);// Initialize
  // Exists a view for that document type?
  CView *pView = pDoc->GetNextView(Pos);
  if( NULL == pView )// No
    { m_pTplteOnlyOneView->OpenDocumentFile(NULL);// Create view
  //Exists a window for that view?
  CWnd *pWnd = pView->GetParent();
  if( NULL == pWnd )//No
    { m_pTplteOnlyOneView->OpenDocumentFile(NULL);// Create view
  // A View and a window exists. Restore or activate?
  if( SW_SHOWMINIMIZED  == wpl.showCmd )// Iconized window must be restored
    { wpl.showCmd = SW_RESTORE;
      if( WPF_RESTORETOMAXIMIZED == wpl.flags )
         wpl.showCmd = SW_SHOWMAXIMIZED;
    { wpl.showCmd = SW_RESTORE; // Bring window to top
That does the trick.If you want to open this document with a file, provide the filename to the <FONT size=2>OpenDocumentFile</FONT> function.  Of course then you also have to take care of the contents of the old view, before filling in a new one.

If you have more document types, where only one view should be allowed, just add another CMultiDocTemplate* variable, another command handler, paste the code and adjust the names.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

G. Steudtel
Software Developer (Senior)
Germany Germany
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionEasier Way? Pin
Clevedon_Peanut28-Oct-03 10:14
memberClevedon_Peanut28-Oct-03 10:14 
GeneralMulti View Pin
Vaclav30-Jun-03 19:48
memberVaclav30-Jun-03 19:48 
GeneralGood Pin
Kant24-Feb-03 9:20
memberKant24-Feb-03 9:20 
GeneralRe: Good Pin
G. Steudtel25-Feb-03 5:26
memberG. Steudtel25-Feb-03 5:26 
GeneralRe: Good Pin
secret_agent10-Mar-04 5:48
membersecret_agent10-Mar-04 5:48 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170525.1 | Last Updated 25 Feb 2003
Article Copyright 2003 by G. Steudtel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid