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

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jun 19, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.