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.

Downloads

Download demo project - 160 Kb

History



Comments

  • 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??

    Reply
  • 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.

    Reply
  • 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??

    Reply
  • 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.
    Rafal

    Reply
  • Thanks

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

    Originally posted by: DK

    cool trick

    Reply
  • Great!!

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

    Originally posted by: Jianguo Wang

    Thanks!

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

    Take care!

    Jianguo

    Reply
  • 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.

    Reply
  • It's cool....

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

    Originally posted by: TaeSeo Kim

    Thanks...

    It's cool...

    Reply
  • 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(
    IDR_MAINFRAME,
    RUNTIME_CLASS(CMyDoc),
    RUNTIME_CLASS(CFrame1),
    RUNTIME_CLASS(CView1));

    m_mapDocTemplates["MyFrame2"] = new CMultiDocTemplate(
    IDR_MAINFRAME,
    RUNTIME_CLASS(CMyDoc),
    RUNTIME_CLASS(CFrame2),
    RUNTIME_CLASS(CView2));
    ...
    //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;

    Reply
  • Very useful, thanks!

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

    Originally posted by: malc

    Very useful, thanks!

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds