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.

By , 14 Nov 2003
 

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

About the Author

rj45
Software Developer (Senior)
Canada Canada
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionCreation of Multiple Dynamic Viewsmemberpoonampawar111 Aug '10 - 21:09 
GeneralProblem ...memberEnzo146915 Oct '08 - 10:49 
Generalwhy do u use OpenDocument for each Doc template.membermostafa_pasha11 Sep '06 - 22:41 
Questionfinally - why is it so hard to find how to do things?membernadiric21 Aug '06 - 17:15 
QuestionHow to get the correct document to open?memberSharonTseng13 Oct '04 - 3:23 
AnswerRe: How to get the correct document to open?memberrm_pkt14 Nov '06 - 1:39 
Generalregarding variables in Multiple Dynamic Viewsmembercdranger19 May '04 - 4:41 
GeneralRe: regarding variables in Multiple Dynamic Viewsmemberkhecimovic31 Aug '04 - 3:45 
GeneralOLD articlemembermlsh21 Nov '03 - 6:18 
GeneralTHANKSmembernevedko1 Jan '04 - 16:57 
GeneralRe: THANKSmemberrj452 Jan '04 - 14:53 
GeneralRe: THANKSmember*Dreamz16 Apr '04 - 3:36 
GeneralRe: THANKSmembermlsh11 Jan '04 - 7:22 
GeneralMultiple View Problemsmemberrubberband18 Nov '03 - 4:39 
GeneralRe: Multiple View Problemsmemberrubberband18 Nov '03 - 6:04 
GeneralRe: Multiple View Problemsmemberrj4511 Jan '04 - 7:37 
GeneralYou said it was for VC++ 6.memberWREY16 Nov '03 - 15:52 
GeneralRe: You said it was for VC++ 6.sussAnonymous17 Nov '03 - 4:54 
GeneralLINKS FIXED&gt;memberrj4515 Nov '03 - 6:36 
GeneralBroken LinksmemberDouglas Macintosh29 Oct '03 - 13:49 
GeneralRe: Broken Linksmemberrj453 Nov '03 - 13:22 
GeneralLink does not workmemberRichard Dalling26 Oct '03 - 21:08 
GeneralRe: Link does not workmemberrj4527 Oct '03 - 13:21 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 15 Nov 2003
Article Copyright 2003 by rj45
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid