| Tenouk C & C++ | MFC Home | OLE & Containers 2 | OLE & Containers 4 | Download | Site Index |


 

 

 

 

 

 

OLE Embedded Components and Containers Part 3

 

 

 

 

 

 

Program examples compiled using Visual C++ 6.0 compiler on Windows XP Pro machine with Service Pack 2. The Excel version is Excel 2003/Office 11. Topics and sub topics for this tutorial are listed below. Don’t forget to read Tenouk’s small disclaimer. The supplementary notes for this tutorial are IOleObject and OLE.

  1. The EX32B Example: An Embedding Container

  2. The EX32B From Scratch

 

 

The EX32B Project Example: An Embedding Container

 

Now we can move on to the working program. It's a good time to open the Ex32b.dsw workspace and build the EX32B project. If you choose Insert Object from the Edit menu and select Ex32a Document, the EX32A component will start. If you change the component's data, the container and the component will look something like this.

 

Figure 5: EX32B in action.

 

Figure 5: EX32B in action.

 

The EX32B From Scratch

 

This is an MDI application without Automation and ActiveX Controls support. The View’s base class is CScrollView.

 

Figure 6: EX32B – Visual C++ new project dialog.

 

Figure 6: EX32B – Visual C++ new project dialog.

 

Select Multiple documents option.

 

Figure 7: EX32B – AppWizard step 1 of 6.

 

Figure 7: EX32B – AppWizard step 1 of 6.

 

Figure 8: EX32B – AppWizard step 2 of 6.

 

Figure 8: EX32B – AppWizard step 2 of 6.

 

Deselect the Automation and ActiveX Controls options.

 

Figure 9: EX32B – AppWizard step 3 of 6.

 

Figure 9: EX32B – AppWizard step 3 of 6.

 

Figure 10: EX32B – AppWizard step 4 of 6.

 

Figure 10: EX32B – AppWizard step 4 of 6.

 

Figure 11: EX32B – AppWizard step 5 of 6.

 

Figure 11: EX32B – AppWizard step 5 of 6.

 

Change the view base class to CScrollView.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 12: EX32B – AppWizard step 6 of 6.

 

Figure 12: EX32B – AppWizard step 6 of 6.

 

Figure 13: EX32B project summary.

 

Figure 13: EX32B project summary.

 

Add the following menu items under the existing Edit menu.

 

ID

Caption

ID_EDIT_CLEAR_ALL

Clear All (replacing the Undo)

ID_EDIT_COPYTO

Copy To

ID_EDIT_PASTEFROM

Paste From

ID_EDIT_INSERTOBJECT

Insert Object

 

Table 12.

 

Figure 14: Replacing Undo with Clear All menu item.

 

Figure 14: Replacing Undo with Clear All menu item.

 

Figure 15: Adding Separator.

 

Figure 15: Adding Separator.

 

Figure 16: Adding Copy To menu item.

 

Figure 16: Adding Copy To menu item.

 

Figure 17: Adding Paste From menu item.

 

Figure 17: Adding Paste From menu item.

 

Figure 18: Adding Separator.

 

Figure 18: Adding Separator.

 

Figure 19: Adding Insert Object menu item.

 

Figure 19: Adding Insert Object menu item.

 

Figure 20: A completed EX32B menu items.

 

Figure 20: A completed EX32B menu items.

 

 

Use ClassWizard to add the following message handlers to CEx32bView class. Take note that ID_EDIT_COPY, ID_EDIT_CUT and ID_EDIT_COPYTO having same update command handler.

 

ID

Type

Handler

ID_EDIT_COPY

COMMAND

OnEditCopy()

ID_EDIT_COPY

UPDATE COMMAND

OnUpdateEditCopy()

ID_EDIT_CUT

COMMAND

OnEditCut()

ID_EDIT_CUT

UPDATE COMMAND

OnUpdateEditCopy()

ID_EDIT_PASTE

COMMAND

OnEditPaste()

ID_EDIT_PASTE

UPDATE COMMAND

OnUpdateEditPaste()

ID_EDIT_COPYTO

COMMAND

OnEditCopyto()

ID_EDIT_COPYTO

UPDATE COMMAND

OnUpdateEditCopy()

ID_EDIT_INSERTOBJECT

COMMAND

OnEditInsertobject()

ID_EDIT_INSERTOBJECT

UPDATE COMMAND

OnUpdateEditInsertobject()

ID_EDIT_PASTEFROM

COMMAND

OnEditPasteFrom()

 

 

 

 

Table 13.

 

 

Figure 21: Adding update command handler for ID_EDIT_COPYTO.

 

Figure 21: Adding update command handler for ID_EDIT_COPYTO.

 

Figure 22: Adding update command handler for ID_EDIT_CUT.

 

Figure 22: Adding update command handler for ID_EDIT_CUT.

 

Add the following Window message handlers to CEx32bView class.

 

Message

WM_LBUTTONDBLCLK

WM_LBUTTONDOWN

WM_SETCURSOR

 

Table 14.

 

Figure 23: Adding Window message handlers to CEx32bView class.

 

Figure 23: Adding Window message handlers to CEx32bView class.

 

You can verify the added handlers in ex32bView.h as shown below.

 

OLE Embedded Components and Containers - MFC C++ code snippet

 

Listing 1.

 

Add/override the following message handlers to CEx32bDoc class.

 

DeleteContents()

OnCloseDocument()

SaveModified()

 

Figure 24: Adding message handlers to CEx32bDoc class.

 

Figure 24: Adding message handlers to CEx32bDoc class.

 

OLE Embedded Components and Containers - MFC C++ code snippet

 

Listing 2.

 

Add the following message map for the Clear All menu to the CEx32bDoc class.

 

ID

 

Handler

ID_EDIT_CLEAR_ALL

COMMAND

OnEditClearAll

 

Table 15.

 

Figure 25: Adding message map for the Clear All menu to the CEx32bDoc class.

 

Figure 25: Adding message map for the Clear All menu to the CEx32bDoc class.

 

The ClassWizard added code is shown below.

 

OLE Embedded Components and Containers - MFC C++ code snippet

 

Listing 3.

 

Add the following #define directives in ex32bView.h.

 

#define CF_OBJECTDESCRIPTOR   "Object Descriptor"

#define CF_EMBEDDEDOBJECT "Embedded Object"

#define SETFORMATETC(fe, cf, asp, td, med, li)   \

      ((fe).cfFormat=cf, \

       (fe).dwAspect=asp, \

       (fe).ptd=td, \

       (fe).tymed=med, \

       (fe).lindex=li)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

----------------------------------------------------------------------------------------------------------------------------------------

OLE Embedded Components and Containers - MFC C++ code snippet

 

Listing 4.

 

Then, add the following public member variables.

 

public:

      CLIPFORMAT m_cfObjDesc;

      CLIPFORMAT m_cfEmbedded;

      CSize m_sizeTotal;  // document size

      CRectTracker m_tracker;

      CRect m_rectTracker; // logical coords

 

OLE Embedded Components and Containers - MFC C++ code snippet

 

Listing 5.

 

Use ClassView, add the following private member functions to CEx32bView class.

 

private:

      void GetSize();

      void SetNames();

      void SetViewAdvise();

      BOOL MakeMetafilePict(COleDataSource* pSource);

      COleDataSource* SaveObject();

      BOOL DoPasteObject(COleDataObject* pDataObject);

      BOOL DoPasteObjectDescriptor(COleDataObject* pDataObject);

 

Figure 26: Add new member function using ClassView.

 

Figure 26: Add new member function using ClassView.

 

Figure 27: Adding CEx32bView’s GetSize() member function.

 

Figure 27: Adding CEx32bView’s GetSize() member function.

 

You can verify the added member functions in ex32bView.h file.

 

OLE Embedded Components and Containers - MFC C++ code snippet

 

Listing 6.

 

 

 

 

 

 

Further reading and digging:

  1. DCOM at MSDN.

  2. COM+ at MSDN.

  3. COM at MSDN.

  4. Win32 process, thread and synchronization story can be found starting from Module R.

  5. MSDN MFC 7.0 class library online documentation.

  6. MSDN MFC 9.0 class library online documentation - latest version.

  7. MSDN Library

  8. Windows data type.

  9. Win32 programming Tutorial.

  10. The best of C/C++, MFC, Windows and other related books.

  11. Unicode and Multibyte character set: Story and program examples.

 

 

 

 

 

 


 

| Tenouk C & C++ | MFC Home | OLE & Containers 2 | OLE & Containers 4 | Download | Site Index |