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.
|
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. |
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.
Select Multiple documents option.
Figure 7: EX32B – AppWizard step 1 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 10: EX32B – AppWizard step 4 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 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 15: Adding Separator.
Figure 16: Adding Copy To menu item.
Figure 17: Adding Paste From menu item.
Figure 18: Adding Separator.
Figure 19: Adding Insert Object menu item.
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 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.
You can verify the added handlers in ex32bView.h as shown below.
Listing 1.
Add/override the following message handlers to CEx32bDoc class.
DeleteContents()
OnCloseDocument()
SaveModified()
Figure 24: Adding message handlers to CEx32bDoc class.
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.
The ClassWizard added code is shown below.
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)
----------------------------------------------------------------------------------------------------------------------------------------
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
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 27: Adding CEx32bView’s GetSize() member function.
You can verify the added member functions in ex32bView.h file.
Listing 6.
Further reading and digging:
Win32 process, thread and synchronization story can be found starting from Module R.
MSDN What's New (MFC Feature Pack) - feature pack.
DCOM at MSDN.
COM+ at MSDN.
COM at MSDN.
Unicode and Multi-byte character set: Story and program examples.