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:
以上是老外的例子,我已经试验成功,需要的话可以发个email给我