Click to See Complete Forum and Search --> : How can I implement a ActiveX Control property like Picture


June 4th, 1999, 05:17 AM
How can I implement a ActiveX Control property like Picture control's picture property? ATL & MFC

mailto me: 21newtimes@163.net

June 4th, 1999, 10:47 AM
This is part of the help of Microsoft Visual Studio.
The Section is "ActiveX Controls: Using Pictures in an ActiveX Control"
If you complete these stapes you will get a Picture property in
your Activex Control Object.

Jesús Castillo
e-mail to: jesusC@etheron.net

Implementing a Custom Picture Property in Your ActiveX Control

When you have completed the steps outlined in this section, the control can display pictures chosen by its user. The user can change the displayed picture using a property page that shows the current picture and has a Browse button that allows the user to the select different pictures.

A custom Picture property is implemented using a process similar to that used for implementing other properties, the main difference being that the custom property must support a Picture type. Because the item of the Picture property must be drawn by the ActiveX control, a number of additions and modifications must be made to the property before it can be fully implemented.

To implement a custom Picture property, you must do the following:

1.- Add code to your control project.

A standard Picture property page ID, a data member of type CPictureHolder, and a custom property of type LPPICTUREDISP with a Get/Set implementation must be added.

2.- Modify several functions in your control class.

These modifications will be made to several functions that are responsible for the drawing of your ActiveX control.

1.- Additions to Your Control Project

To add the property page ID for the standard Picture property page, insert the following line after the BEGIN_PROPPAGEIDS macro in the control implementation file (.CPP):

PROPPAGEID(CLSID_CPicturePropPage)

You must also increment the count parameter of your BEGIN_PROPPAGEIDS macro by one. The following line illustrates this:

BEGIN_PROPPAGEIDS(CSampleCtrl, 2)

To add the CPictureHolder data member to the control class, insert the following line under the protected section of the control class declaration in the control header file (.H):

CPictureHolder m_pic;

It is not necessary to name your data member m_pic; any name will suffice.

Next, add a custom property that supports a Picture type:

To add a custom picture property using ClassWizard

With your control project open, open ClassWizard by clicking ClassWizard on the View menu.


Click the Automation tab.


Click Add Property.


In the External name box, type the property name. For example purposes, ControlPicture is used in this procedure.


In the Implementation box, click Get/Set Methods.


In the Return Type box, select LPPICTUREDISP for the property type.


Type unique names for your Get and Set Functions or accept the default names. (In this example, the default names GetControlPicture and SetControlPicture are used.)


Click OK to close the Add Property dialog box.


Click OK to confirm your choices and close ClassWizard.
ClassWizard adds the following code between the dispatch map comments in the control header (.H) file:

afx_msg LPPICTUREDISP GetControlPicture();
afx_msg void SetControlPicture(LPPICTUREDISP newValue);

In addition, the following code was inserted in the dispatch map of the control implementation (.CPP) file:

DISP_PROPERTY_EX(CSampleCtrl, "ControlPicture", GetControlPicture, SetControlPicture, VT_PICTURE)

ClassWizard also adds the following two stub functions in the control implementation file:

LPPICTUREDISP CSampleCtrl::GetControlPicture()
{
// TODO: Add your property handler here

return NULL;
}

void CSampleCtrl::SetControlPicture(LPPICTUREDISP newValue)
{
// TODO: Add your property handler here

SetModifiedFlag();
}

Note Your control class and function names might differ from the example above.

2.- Modifications to Your Control Project

Once you have made the necessary additions to your control project, you need to modify several functions that affect the rendering of your ActiveX control. These functions, OnResetState, OnDraw, and the Get/Set functions of a custom Picture property, are located in the control implementation file. (Note that in this example the control class is called CSampleCtrl, the CPictureHolder data member is called m_pic, and the custom picture property name is ControlPicture.)

In the control OnResetState function, add the following optional line after the call to COleControl::OnResetState:

m_pic.CreateEmpty();

This sets the control’s picture to a blank picture.

To draw the picture properly, make a call to CPictureHolder::Render in the control OnDraw function. Modify your function to resemble the following example:

void CSampleCtrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
// ****** Add your code below this line ********** //
m_pic.Render(pdc, rcBounds, rcBounds);
}

In the Get function of the control’s custom picture property, add the following line:

return m_pic.GetPictureDispatch();

In the Set function of the control’s custom Picture property, add the following lines:

m_pic.SetPictureDispatch(newValue);
InvalidateControl();

The picture property must be made persistent so that information added at design time will show up at run time. Add the following line to the COleControl-derived class's DoPropExchange function:

PX_Picture(pPX, "ControlPicture",m_pic);

Note Your class and function names might differ from the example above.