Click here to Skip to main content
Click here to Skip to main content

Creation of Multiple Dynamic Views. How to initilize and use additional different views in your MDI app by using Doc / view architecture. Example code of OpenGL window and a Dialog window.

, 14 Nov 2003
Rate this:
Please Sign up or sign in to vote.
An MDI MFC generated project has only one view by default, here is one way to add more CView derived classes and initiliaze them without adding any special initialization code.

Introduction

The example code has a OpenGL view which is derived from CView and also a Dialog View that was created by the App Wizard upon the project's creation.

How do I get multiple views with my MDI MFC application? Well, first I read an article here which used a tab control and created instances of CView derived classes and placed them within the tab dialog. That worked, but I didn't want my views in a tab dialog. Here is another way to do it, without building your own initialization code.

I wanted to have an OpenGL View and a Dialog View on my MDI application. Normally, the MDI app just has one view derived from CView and we all know you can make multiple instances of that view by going File-New. But what if you want to have two windows, etc., OpenGL and a dialog view? You have to make two classes derived from CView and place them into the CMultiDocTemplate.

So startup the App Wizard and choose the CFormView View class instead of CView and make sure you selected MDI instead of SDI. Always use MDI because, hey why not? You can limit the windows the user creates but you can also add new views easily if later needed. Now, add in the OpenglEnabledView class by adding the .h and .cpp. (Sample is available on CodeProject, check for details on the usage. The files are also in my project but not mine.)

In your project, find the app class, then go to the initinstance method. Search for this:

//////////start snip
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(IDR_NeuralNetTwoTYPE,
RUNTIME_CLASS(CNeuralNetTwoDoc),
// custom MDI child frame
RUNTIME_CLASS(CChildFrame),
//this is the default view created by app wizard.
RUNTIME_CLASS(CGLEnabledView));
AddDocTemplate(pDocTemplate);
////endsnip

So what you want to do is add a new template to the template manager. To do that just duplicate that and add in your other view class derived from CView.

///start snip
CMultiDocTemplate* pDocTemplate2; //notice: 2 -<<
pDocTemplate2 = new CMultiDocTemplate(IDR_NeuralNetTwoTYPE,
RUNTIME_CLASS(CNeuralNetTwoDoc),
// custom MDI child frame
RUNTIME_CLASS(CChildFrame),
////NOTICE!!!: that this is a diffrent cview class.
RUNTIME_CLASS(CNeuralNetTwoView));
AddDocTemplate(pDocTemplate2);
//end snip

Now, at this point, when you go File-New, you will get a choice of what document template you want to use. This doesn't look very professional and if you want both dialogs views to open automatically, you will have to add some code. To remove that stupid box, add this after the command info line on the app class' initinstance method:

CCommandLineInfo cmdInfo; //after this stock appwiz generated. <-
cmdInfo.m_nShellCommand=CCommandLineInfo::FileNothing ; 
//add this to stop teh default from File_new from 
//getting called and bringing up that dialog.

Now, override file_new message handler from the CDocument class. To do that, add a message map for File_new to your mainframe class. In VS.NET, you change the properties by right clicking on the class in class view and selecting Properties, then hit the lightning icon. In VS6, you use the class wizard to map messages.

//snipstart
void CMainFrame::OnFileNew()
{
    for(POSITION tPos = 
      theApp.m_pDocManager->GetFirstDocTemplatePosition();tPos!=NULL;)
    {
        //get pointer to the CdocTemplates.
        CDocTemplate * ptempDocTemplate = 
              theApp.m_pDocManager->GetNextDocTemplate (tPos);
        //this will make the view visible.
        ptempDocTemplate->OpenDocumentFile(NULL);
    }
}//snipend

The document templates hold the frame, doc, and view, and you can access those objects through the DocTemplate pointer which we got from the document manager pointer from our theApp global. The OpenDocumentFile method of CDocument will init the objects for you and also store them in the document manager which has a pointer to it from the derived class CwinApp.

You also will have to add message handler code for the File_New message on your doc class. You will add the exact same code so that the child frame will also open both the views.

//snip start
void CNeuralNetTwoDoc::OnFileNew()
{
    for(POSITION tPos = 
      theApp.m_pDocManager->GetFirstDocTemplatePosition();tPos!=NULL;)
    {
         CDocTemplate * ptempDocTemplate = 
           theApp.m_pDocManager->GetNextDocTemplate (tPos);
         ptempDocTemplate->OpenDocumentFile(NULL);
    }
}//snip end

That code will call OpenDocumentFile on all the documents you have in the template list which will init everything for you.

Questions and comments: please email r_jay_thompson@hotmail.com.

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

Share

About the Author

rj45
Software Developer (Senior)
Canada Canada
No Biography provided

Comments and Discussions

 
QuestionCreation of Multiple Dynamic Views Pinmemberpoonampawar111-Aug-10 22:09 
GeneralProblem ... PinmemberEnzo146915-Oct-08 11:49 
Generalwhy do u use OpenDocument for each Doc template. Pinmembermostafa_pasha11-Sep-06 23:41 
Questionfinally - why is it so hard to find how to do things? Pinmembernadiric21-Aug-06 18:15 
QuestionHow to get the correct document to open? PinmemberSharonTseng13-Oct-04 4:23 
AnswerRe: How to get the correct document to open? Pinmemberrm_pkt14-Nov-06 2:39 
Generalregarding variables in Multiple Dynamic Views Pinmembercdranger19-May-04 5:41 
GeneralRe: regarding variables in Multiple Dynamic Views Pinmemberkhecimovic31-Aug-04 4:45 
GeneralOLD article Pinmembermlsh21-Nov-03 7:18 
GeneralTHANKS Pinmembernevedko1-Jan-04 17:57 
GeneralRe: THANKS Pinmemberrj452-Jan-04 15:53 
GeneralRe: THANKS Pinmember*Dreamz16-Apr-04 4:36 
GeneralRe: THANKS Pinmembermlsh11-Jan-04 8:22 
GeneralMultiple View Problems Pinmemberrubberband18-Nov-03 5:39 
GeneralRe: Multiple View Problems Pinmemberrubberband18-Nov-03 7:04 
GeneralRe: Multiple View Problems Pinmemberrj4511-Jan-04 8:37 
GeneralYou said it was for VC++ 6. PinmemberWREY16-Nov-03 16:52 
GeneralRe: You said it was for VC++ 6. PinsussAnonymous17-Nov-03 5:54 
GeneralLINKS FIXED&gt; Pinmemberrj4515-Nov-03 7:36 
GeneralBroken Links PinmemberDouglas Macintosh29-Oct-03 14:49 
GeneralRe: Broken Links Pinmemberrj453-Nov-03 14:22 
GeneralLink does not work PinmemberRichard Dalling26-Oct-03 22:08 
GeneralRe: Link does not work Pinmemberrj4527-Oct-03 14:21 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 15 Nov 2003
Article Copyright 2003 by rj45
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid