ComponentOne VSView 8.0
Handling ActiveX Picture Properties in MFC

OLE Pictures are objects in their own right. They have methods that retrieve their size, type, and so on. As such, setting or retrieving Pictureproperties involves dealing with their IDispatch pointers (IDispatch is the basic type of Automation COMinterface). The question is how do I create one of these interfaces to give to the control? The easiest way is through an MFChelper class called CPictureHolder. This class, declared in the AFXCTL.H file, has methods that allow you to create and manage OLE pictures.

The code below shows how you can use the CPictureHolder class with the VSPrintercontrol's DrawPicture method:

Example Title
Copy Code
#include  // declare CPictureHolder class

void CMyDlg::OnButton1()

{

  // start VSPrinter document

  m_Printer.StartDoc();

  m_Printer.SetParagraph("Here is a picture:");

 

  // create CPictureHolder from a bitmap resource

  CPictureHolder pic;

  pic.CreateFromBitmap(IDB_BITMAP1);

 

  // get the LPDISPATCH pointer (must release later)

  LPDISPATCH pPic = pic.GetPictureDispatch();

 

  // draw the picture

  COleVariant v1in("1in");

  COleVariant v3in("3in");

  COleVariant vNone(0L, VT_ERROR);

  m_Printer.DrawPicture(pPic, v1in, v3in,

                        v3in, v3in, vNone, vNone);

 

  // don't forget to release the LPDISPATCH

  pPic->Release();

 

  // finish document

  m_Printer.EndDoc();

}

Besides setting the picture property, the code above illustrates an important point when dealing with COMinterfaces. Notice how the LPDISPATCH pointer is obtained, used, and released. Failing to release COM pointers results in objects dangling in memory and wasting resources. (This type of potential problem is one of the motivations that led to the creation of smart pointers, both the CComPtr ATL and the _com_ptr_t compiler COM support class, which we'll mention later.)

Some properties and methods require that you pass pictures in VARIANT parameters (for example, the Cell property). To do this, initialize a COleVariant as follows:

Example Title
Copy Code
COleVariant vPic;

V_VT(&vPic) = VT_DISPATCH;

V_DISPATCH(&vPic) = pic.GetPictureDispatch();

Notice that in this case you must not release the LPDISPATCH pointer, because the COleVariant destructor will do that automatically when vPic goes out of scope.

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback