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: May 6, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT While you likely have very good reasons for remaining on WinXP after end of support -- an estimated 20-30% of worldwide devices still are -- the bottom line is your security risk is now significant. In the absence of security patches, attackers will certainly turn their attention to this new opportunity. Join Lumension Vice President Paul Zimski in this one-hour webcast to discuss risk and, more importantly, 5 pragmatic risk mitigation techniques …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds