OLE Pictures are objects in their own right. They have methods that retrieve their size, type, and so on. As such, setting or retrieving Picture properties involves dealing with their IDispatch pointers (IDispatch is the basic type of Automation COM interface). The question is, how do I create one of these interfaces to give to the control? The easiest way is through an MFC helper 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 to set the VSFlexGrid control's CellPicture property:
#include <afxctl.h> // declare CPictureHolder class
void CJnkDlg::OnButton1()
{
// create CPictureHolder
CPictureHolder pic;
// load a picture from a bitmap resource
pic.CreateFromBitmap(IDB_BITMAP1);
// get the LPDISPATCH pointer
LPDISPATCH pPic = pic.GetPictureDispatch();
// assign LPDISPATCH to control
m_Grid.SetCellPicture(pPic);
// don't forget to release the LPDISPATCH
pPic->Release();
}
Besides setting the picture property, the code above illustrates an important point when dealing with COM interfaces. Notice how the LPDISPATCH pointer is obtained, used, and released. Failing to release COM pointers results in objects dangling in memory and wasting resources.
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:
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.