This documentation is archived and is not being maintained.

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 Application Wizard, then modify the application to support active document containment.

To create an active document container application

  1. From the File menu, click Project from the New submenu.

  2. From the left pane, click Visual C++ Projects.

  3. Select MFC Application from the right pane.

  4. Name the project MyProj.

  5. Select the Compound Document Support page.

  6. Select the Container or Container/Full-server option.

  7. Select the Active document container check box.

  8. Click Finish.

  9. When the MFC Application Wizard finishes generating the application, open the following files using Solution Explorer:

    • Cntritem.cpp

    • Cntritem.h

    • MyProjview.cpp

    • MyProjview.h

  10. 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 files generated by the MFC Application Wizard are 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.

  11. In Cntritem.h, make the following changes:

    • Replace COleClientItem with COleDocObjectItem

  12. 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
            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.

  13. In MyProjview.h, make the following changes:

    • Add the following to Overrides:

      virtual void OnPrint(CDC* pDC, CPrintInfo* pInfo);

    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.


    COleDocument maintains a list of COleClientItem objects, not COleDocObjectItem objects.

  14. Add any implementation of your own and build the application.

See Also