Click here to Skip to main content
15,310,940 members
Articles / Desktop Programming / MFC
Posted 24 Oct 2003


36 bookmarked

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.

Rate me:
Please Sign up or sign in to vote.
2.54/5 (30 votes)
14 Nov 20033 min read
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.


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,
// custom MDI child frame
//this is the default view created by app wizard.

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,
// custom MDI child frame
////NOTICE!!!: that this is a diffrent cview class.
//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.

void CMainFrame::OnFileNew()
    for(POSITION tPos = 
        //get pointer to the CdocTemplates.
        CDocTemplate * ptempDocTemplate = 
              theApp.m_pDocManager->GetNextDocTemplate (tPos);
        //this will make the view visible.

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 = 
         CDocTemplate * ptempDocTemplate = 
           theApp.m_pDocManager->GetNextDocTemplate (tPos);
}//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


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

Software Developer (Senior)
Canada Canada
No Biography provided

Comments and Discussions

QuestionCreation of Multiple Dynamic Views Pin
poonampawar111-Aug-10 21:09
Memberpoonampawar111-Aug-10 21:09 
GeneralProblem ... Pin
Enzo146915-Oct-08 10:49
MemberEnzo146915-Oct-08 10:49 
Generalwhy do u use OpenDocument for each Doc template. Pin
mostafa_pasha11-Sep-06 22:41
Membermostafa_pasha11-Sep-06 22:41 
Questionfinally - why is it so hard to find how to do things? Pin
nadiric21-Aug-06 17:15
Membernadiric21-Aug-06 17:15 
QuestionHow to get the correct document to open? Pin
SharonTseng13-Oct-04 3:23
MemberSharonTseng13-Oct-04 3:23 
AnswerRe: How to get the correct document to open? Pin
rm_pkt14-Nov-06 1:39
Memberrm_pkt14-Nov-06 1:39 
Generalregarding variables in Multiple Dynamic Views Pin
Member 93355919-May-04 4:41
MemberMember 93355919-May-04 4:41 
GeneralRe: regarding variables in Multiple Dynamic Views Pin
l00p1n631-Aug-04 3:45
Memberl00p1n631-Aug-04 3:45 
GeneralOLD article Pin
mlsh21-Nov-03 6:18
Membermlsh21-Nov-03 6:18 
GeneralTHANKS Pin
nevedko1-Jan-04 16:57
Membernevedko1-Jan-04 16:57 
GeneralRe: THANKS Pin
rj452-Jan-04 14:53
Memberrj452-Jan-04 14:53 
GeneralRe: THANKS Pin
*Dreamz16-Apr-04 3:36
Member*Dreamz16-Apr-04 3:36 
GeneralRe: THANKS Pin
mlsh11-Jan-04 7:22
Membermlsh11-Jan-04 7:22 
GeneralMultiple View Problems Pin
rubberband18-Nov-03 4:39
Memberrubberband18-Nov-03 4:39 
GeneralRe: Multiple View Problems Pin
rubberband18-Nov-03 6:04
Memberrubberband18-Nov-03 6:04 
GeneralRe: Multiple View Problems Pin
rj4511-Jan-04 7:37
Memberrj4511-Jan-04 7:37 
GeneralYou said it was for VC++ 6. Pin
WREY16-Nov-03 15:52
MemberWREY16-Nov-03 15:52 
GeneralRe: You said it was for VC++ 6. Pin
Anonymous17-Nov-03 4:54
MemberAnonymous17-Nov-03 4:54 
GeneralLINKS FIXED&gt; Pin
rj4515-Nov-03 6:36
Memberrj4515-Nov-03 6:36 
GeneralBroken Links Pin
Douglas Macintosh29-Oct-03 13:49
MemberDouglas Macintosh29-Oct-03 13:49 
GeneralRe: Broken Links Pin
rj453-Nov-03 13:22
Memberrj453-Nov-03 13:22 
GeneralLink does not work Pin
bishbosh0226-Oct-03 21:08
Memberbishbosh0226-Oct-03 21:08 
GeneralRe: Link does not work Pin
rj4527-Oct-03 13:21
Memberrj4527-Oct-03 13: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.