Creating an (MDI) app that ALWAYS displays two views of the same document

This demo app shows how to create an (MDI) app whereby opening or creating a new document automatically shows both views. The only drawback to this is that if you use the New Window item on the Window menu, it always creates just one view - the first type. I'm sure that can be resolved by someone if necessary.

Also, closing one child frame will not automatically close the other - this behaviour might be desirable if the user only wants to see the one view; if not, then that will need some work.

If anyone feels that these are important, let me know and I will work on them.

Steps involved:

1. Create a second view class for the second type of data display (using ClassWizard).

2. Construct a second CMultiDocTemplate in CWinApp::InitInstance(), using the second view class and the same document and child frame as the first. Store its pointer in the app class, but do NOT call CWinApp::AddDocTemplate() with this one - otherwise the app will ask the user what type of doc to create whenever you try to create a new document.

3. In CWinApp::ExitInstance(), delete this second doc template as it does not get automatically deleted like the first one does (because it was not added to the app).

4. In the document's OnNewDocument(), get a pointer to the second template from the app object, and then call CreateNewFrame() and InitialUpdateFrame() (in that order) with this doc template; use the document's 'this' pointer for the document to use (so that the second view shares the first document), and NULL for the frame window (since the first MDI child frame does not exist when OnNewDocument() is called - this does not matter). The frame pointer to use in InitialUpdateFrame() is the return value from CreateNewFrame().

Note: As both views share the same document, OnNewDocument() only gets called once per document - therefore the above method of automatically creating the second frame will not cause any problems.

5. Each view can now do its own drawing based on the data in the document. Note that the second view will refer to a generic CDocument pointer, which you will have to cast to the correct document class.


Download demo project - 160 Kb



  • Tiling??

    Posted by toofani on 07/08/2005 04:49am

    Thanks a lot for the code. I helped me a lot. How to tile them automatically, when the two views are displayed on opening a document??

  • No /res Dir

    Posted by Legacy on 09/15/2003 12:00am

    Originally posted by: Sreeram

    I have Downloaded this demo project but i chould not be able to run the application since the zip file didnt have the /res directory.

  • How about two views and two documents??:>

    Posted by Legacy on 03/24/2003 12:00am

    Originally posted by: Sit

    How about two views and two documents??

  • Thanks, I built my app based on this example.

    Posted by Legacy on 11/25/2002 12:00am

    Originally posted by: Rafal

    Thanks, I built my app based on this example.

  • Thanks

    Posted by Legacy on 10/18/2002 12:00am

    Originally posted by: DK

    cool trick

  • Great!!

    Posted by Legacy on 07/01/2002 12:00am

    Originally posted by: Jianguo Wang


    After looked around for a while, I finally found the correct one to help me out.........

    Take care!


  • Creating an (MDI) app that ALWAYS displays two views of the same document

    Posted by Legacy on 02/04/2002 12:00am

    Originally posted by: Gasparote

    Hi Jason,

    It's really a nice article, but how can my MDI application displays allways two views using their own document (Multiple Views and Multiple Docs)?

    Thanks in advance.

  • It's cool....

    Posted by Legacy on 01/26/2002 12:00am

    Originally posted by: TaeSeo Kim


    It's cool...

  • You saved me tonight, Thank you! Use std::map to save more DocTemplates.

    Posted by Legacy on 12/04/2001 12:00am

    Originally posted by: LiBP

    //I defined 
    typedef std::map<CString, CMultiDocTemplate *> mapDocTemplates_t;
    //and, add one variable in app.h
    mapDocTemplates_t m_mapDocTemplates;
    //in app::InitInstance()
    m_mapDocTemplates["MyFrame1"] = new CMultiDocTemplate(

    m_mapDocTemplates["MyFrame2"] = new CMultiDocTemplate(
    //So, multi frames can be show!
    //Don't forget, add following lines in App::ExitInstance()
    mapDocTemplates_t::iterator it = m_mapDocTemplates.begin();
    for (; it != m_mapDocTemplates.end(); it++)
    delete (*it).second;

  • Very useful, thanks!

    Posted by Legacy on 06/13/2001 12:00am

    Originally posted by: malc

    Very useful, thanks!

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The mobile market is white hot. Building a well-crafted product development plan that addresses market research, strategy, design, and development will provide the greatest chance for success. Each phase of an app's lifecycle is critical to the its overall success and feeds into the next step of the process of product development for the app. This white paper examines the five key phases of mobile app creation, one by one, to understand how they work together to help create a successful mobile app.

  • On-demand Event Event Date: February 12, 2015 The evolution of systems engineering with the SysML modeling language has resulted in improved requirements specification, better architectural definition, and better hand-off to downstream engineering. Agile methods have proven successful in the software domain, but how can these methods be applied to systems engineering? Check out this webcast and join Bruce Powel Douglass, author of Real-Time Agility, as he discusses how agile methods have had a tremendous …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date