Adding a View and Document to an MDI Application
It is possible to add a View/Document from a previous application to an MDI
application. Assume that
-
The View class is
CPrevView
and is defined in PrevView.h
and PrevView.cpp.
-
The Document class is
CPrevDoc
and is defined in
PrevDoc.h and PrevDoc.cpp.
Adding the classes to the Application
-
Copy the classes from the previous application into the current application's
directory.
-
From Solution Explorer of the current app, right click the app name
-
Select Add
-
Select Add Existing Item...
-
Select the .cpp and .h file files for the view and doc and
click Open
Add a String Resource
-
Edit the string table in the current app
-
Add a resource for the new doc/view. In this example, it will be named
IDR_PREV_TYPE
-
Set teh contents of this resource (use the IDR for the current view as a
template)
-
Name of application
-
Default name for file save
-
Document type name (not used in this course)
-
Description for file filter
-
File extension
-
Label for registry entry (short name)
-
Description for registry entry (long name)
Add a document template
-
Open InitInstance in the current app.
-
Add a new doc template for the new view and doc
pDocTemplate = new CMultiDocTemplate(
IDR_PREV_TYPE,
RUNTIME_CLASS(CPrevDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CPrevView));
if (!pDocTemplate)
return FALSE;
AddDocTemplate(pDocTemplate);
- Include the headers for the view and doc in the MainFrm.cpp
Include the Current App Header
The old view and doc contain references to the old application header file. Do not copy that header into the current application. Edit the header files for the view and doc, and change the include for the application header so that it includes the current application's header file.
Run the Application
-
When the application opens, a dialog will display for the two templates that
are in the app.
-
If the dialog does not open,
-
It could be due to the fact that a resource that the view or doc needs has
not been copied into the resources of the current application. See below
for more information on this.
-
If the document or a data class uses the template library, then edit
stdafx.h
to include afxtempl.h
.
Adding Other Resources
This is a little trickier, since it requires that the resource.h
and the .rc
file be edited.
Editing resource.h
Editing the Application's.rc
file
-
Open the
.rc
file for the application
-
Copy a dialog definition from the previous application's
.rc
file
into this .rc
file.
-
Copy a menu definition from the previous application's
.rc
file
into this .rc
file.
-
Copy a toolbar definition from the previous application's
.rc
file
into this .rc
file. Be sure that the .bmp
file
is actually in the location that you specify.
-
Be very careful!