- - * - WhiteUnicorn - * - -




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >


TI3300 - Adding an IPersistPropertyBag to Active Controls



This document will explain how to add an IPersistPropertyBag
interface to an ActiveX control. This will allow setting the
properties of an ActiveX control through the use of HTML
PARAM tags. Adding the IPersistPropertyBag interface to an
ActiveX control will also allow it to have its properties set
through the use of tools like the ActiveX Control Pad.

Adding the IPersistPropertyBag interface to an ActiveX control
is a simple process. All that is necessary is to add the
interface to an object's class definition and implement the
interface's three methods. The following example will step you
through this process using a simple ActiveX control based on
TButton. In the example, for the sake of simplicity, this example
will only implement this functionality for the "Caption" property.
It is a simple task to extrapolate this example to provide full
functionality for all properties available in a control.

Begin by using the ActiveX Control Wizard to create a ActiveX
control based on TButton.

Choose File|New and select the ActiveX tab from the New Item
Dialog. Then select ActiveX Control. In the next dialog box
choose TButton for the VCL Class Name. The rest of the defaults
in the dialog box are fine so don't change anything else. Let
Delphi generate the base code for your control.

The next step is to add the IPersistPropertyBag interface to the
class definition. Do this by modifying the first line of the
type declaration from...


type
  TButtonX = class(TActiveXControl, IButtonX)


to...


type
  TButtonX = class(TActiveXControl, IButtonX, IPersistPropertyBag)


Now the IPersistPropertyBag interface has been added to the
type declaration. Next declare the necessary methods by adding
the following to the protected section.


  function IPersistPropertyBag.InitNew = PersistPropBagInitNew;
  function IPersistPropertyBag.Load = PersistPropBagLoad;
  function IPersistPropertyBag.Save = PersistPropBagSave;
  function PersistPropBagInitNew: HResult; stdcall;
  function PersistPropBagLoad(const pPropBag: IPropertyBag;
    const pErrorLog: IErrorLog): HResult; stdcall;
  function PersistPropBagSave(const pPropBag: IPropertyBag;
    fClearDirty: BOOL; fSaveAllProperties: BOOL): HResult;
    stdcall;


Then, of course, implement those functions...


// -- implement PersistPropBagInitNew

function TButtonX.PersistPropBagInitNew: HResult;
begin
  Result := S_OK;
end;

// -- implement PersistPropBagLoad

function TButtonX.PersistPropBagLoad(const pPropBag:
  IPropertyBag; const pErrorLog: IErrorLog): HResult; stdcall;
var
  v: OleVariant;
begin
  if pPropBag.Read('Caption', v, pErrorLog) = S_OK then
    FDelphiControl.Caption := v;
  Result := S_OK;
end;

// -- implement PersistPropBagSave

function TButtonX.PersistPropBagSave(const pPropBag:
  IPropertyBag; fClearDirty: BOOL; fSaveAllProperties: BOOL)
  : HResult; stdcall;
var
  v: OleVariant;
begin
  v:= FDelphiControl.Caption;
  pPropBag.Write('Caption', v);
  Result := S_OK;
end;


With that code added the Control is finished. Go ahead and build
the ActiveX control and deploy it. Do this through the use of the
Web Delpoy Wizard. Just set up the necessary options through
Project|Web Delpoyment Options and deploy through Project|
Web Deploy.

Either you or the Web Deployment Wizard will create an HTML page
which will contain an OBJECT tag something like the following:

<OBJECT
          classid="clsid:324EB783-20A4-11D1-AB11-0020AF3E6306"
          codebase="ActiveX/ButtonXControl.ocx"
          width=100
          height=50
          align=center
          hspace=0
          vspace=0
>
</OBJECT>

This page should work fine. However, now you have the ability
to set the buttons caption through HTML by adding a PARAM tag.
Your new OBJECT tag will look like the following:

<OBJECT
          classid="clsid:324EB783-20A4-11D1-AB11-0020AF3E6306"
          codebase="ActiveX/ButtonXControl.ocx"
          width=100
          height=50
          align=center
          hspace=0
          vspace=0
>
<Param Name="Caption" Value="Hello">
</OBJECT>

The caption of the button will now say "Hello". In this example
only the caption will be accessable through this method. To
expose other properties, follow this example and change the name
of the property you wish to use.



* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >



- - * - Anastasija aka WhiteUnicorn - * - - LJLiveJournal
PFPhotoFile