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

Comments
Tiling??
Posted by toofani on 07/08/2005 04:49amThanks 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??
ReplyNo /res Dir
Posted by Legacy on 09/15/2003 12:00amOriginally 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.
ReplyHow about two views and two documents??:>
Posted by Legacy on 03/24/2003 12:00amOriginally posted by: Sit
How about two views and two documents??
ReplyThanks, I built my app based on this example.
Posted by Legacy on 11/25/2002 12:00amOriginally posted by: Rafal
Thanks, I built my app based on this example.
ReplyRafal
Thanks
Posted by Legacy on 10/18/2002 12:00amOriginally posted by: DK
cool trick
ReplyGreat!!
Posted by Legacy on 07/01/2002 12:00amOriginally posted by: Jianguo Wang
Thanks!
After looked around for a while, I finally found the correct one to help me out.........
Take care!
Jianguo
ReplyCreating an (MDI) app that ALWAYS displays two views of the same document
Posted by Legacy on 02/04/2002 12:00amOriginally 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.
ReplyIt's cool....
Posted by Legacy on 01/26/2002 12:00amOriginally 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:00amOriginally posted by: LiBP
ReplyVery useful, thanks!
Posted by Legacy on 06/13/2001 12:00amOriginally posted by: malc
Very useful, thanks!
ReplyLoading, Please Wait ...