Multiple Views for a Single Document (MDI) | CodeGuru

Multiple Views for a Single Document (MDI)

I add here the code that could be used to add multiple views to a document, in a MDI application. I usually make database applications, and I don’t want that the document to be created/destroyed by the framework, so I create the document myself (and of course I destroy it J ), and in document’s […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 6, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

I add here the code that could be used to add multiple views to a document, in a MDI application. I usually make database applications, and I don’t want that the document to be created/destroyed by the framework, so I create the document myself (and of course I destroy it J ), and in document’s constructor I set the m_bAutoDelete flag to FALSE.

The CMyApp class contains as public member a pointer to my document. I also make document templates for all views, and I keep the pointers in CMyApp. You should have something like this in InitInstance():

m_pView1Templ=new CMultiDocTemplate( IDR_VIEW1TYPE,
RUNTIME_CLASS( CMyDoc ),
RUNTIME_CLASS( CChildFrame ),
RUNTIME_CLASS( CView1 ) );
AddDocTemplate(m_pView1Templ);
// m_pView1Templ is member of CMyApp

After you created all necessary document templates, add the following code:

// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
mp_doc=new CTimeClkDoc;
mp_doc->SetTitle(“Just a demo”);
CCreateContext context;
context.m_pCurrentDoc=mp_doc;
context.m_pNewViewClass=NULL;
context.m_pNewDocTemplate=NULL;
context.m_pLastView=NULL;
context.m_pCurrentFrame=NULL;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME,WS_OVERLAPPEDWINDOW |
                           FWS_ADDTOTITLE,NULL, &context ))
   return FALSE;
m_pMainWnd = pMainFrame;

This code will allow opening the application without any open view and also avoids the dialog box that asks to choose a document template. Add for each view command handlers similar with this one:

void CMyApp::OnView1()
{
   // TODO: Add your command handler code here
   CChildFrame* pFrame = new CChildFrame();
   CCreateContext context;
   context.m_pCurrentDoc=mp_doc;    //that’s the way I avoid creating
                                    //a new document every time I open
                                    //a new view
   context.m_pNewViewClass=RUNTIME_CLASS(CView1);
   context.m_pNewDocTemplate=m_pView1Templ;
   context.m_pLastView=(((CMainFrame *)m_pMainWnd)->
                        GetActiveFrame() ? ((CMainFrame *)m_pMainWnd)->
                        GetActiveFrame()->GetActiveView() : NULL);
   context.m_pCurrentFrame=((CMainFrame *)m_pMainWnd)->GetActiveFrame();
   if (!pFrame->LoadFrame(IDR_VIEW1TYPE,WS_OVERLAPPEDWINDOW |
                          FWS_PREFIXTITLE ,m_pMainWnd, &context ))return;
   pFrame->InitialUpdateFrame(mp_doc,TRUE);
}
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.