Creating an Active Document Container Application
The simplest and most recommended way to create an Active document container application is to create an MFC EXE container application using the MFC AppWizard, then modify the application to support active document containment.
To create an active document container application
From the File menu, choose New.
On the Projects tab, click MFC AppWizard (exe). Name the project MyProj.
In Step 3 of the AppWizard, select the Container or Both container and server button.
Click Finish.
When the AppWizard finishes generating the application, select the FileView tab and open the following files:
Cntritem.cpp
Cntritem.h
MyProjview.cpp
MyProjview.h
In Cntritem.cpp, make the following changes:
Replace COleClientItem with COleDocObjectItem. The latter class augments the OLE code with additional active document containment support.
Remove the implementation code from the body of CMyProjCntrlItem::OnActivate.
The OnActivate code is not needed because the code in a normal container is used to guarantee that the item being activated is the only active item. Therefore, if there is another active item, it is deactivated first. A basic OLE container lets you have multiple embedded objects in a single document, so it needs this code. The default AppWizard-generated active document container is only set up to allow one Active document in the container at a time, so this code is unnecessary. Once you insert an object in your active document container, you cannot insert other objects.
In Cntritem.h, make the following changes:
- Replace COleClientItem with COleDocObjectItem
In MyProjview.cpp, make the following changes:
In CMyProjView::OnPreparePrinting, add the following code:
if (!CView::OnPreparePrinting(pInfo)) return FALSE; if (!COleDocObjectItem::OnPreparePrinting(this, pInfo)) return FALSE; return TRUE;
OnPreparePrinting provides printing support. This code replaces DoPreparePrinting, which is the default print preparation.
Add an OnPrint override as follows:
void CMyProjView::OnPrint(CDC* pDC, CPrintInfo* pInfo) { // TODO: add code to print the controls if(pInfo->m_bDocObject) COleDocObjectItem::OnPrint(this, pInfo, TRUE); }
Active document containment provides an improved printing scheme:
You can first call the Active document through its IPrint interface and tell it to print itself. This is different from previous OLE containment, in which the container had to render an image of the contained item onto the printer CDC object.
If that fails, tell the contained item to print itself through its IOleCommandTarget interface
If that fails, make your own rendering of the item.
The static member functions COleDocObjectItem::OnPrint and COleDocObjectItem::OnPreparePrinting, as implemented in the previous code, handle this improved printing scheme.
In MyProjview.h, make the following changes:
Add the following to Overrides:
virtual void OnPrint(CDC* pDC, CPrintInfo* pInfo);
Note In some functions, you do not need to change COleClientItem to COleDocObjectItem. Because a COleDocObjectItem object is a COleClientItem object, functions that do not specifically implement active document containment functionality (such as OnDestroy, OnCancelEditCntr, OnSetFocus, and OnSize) can implement functionality provided by COM client item objects.
Note also that COleDocument maintains a list of COleClientItem objects, not COleDocObjectItem objects.
- Add any implementation of your own and build the application.