Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

loading ActiveX controls in Delphi (help appreciated)

legacyelegacye Posts: 42Member
Hi,
Im trying to get activeX controls to be created at runtime (for example MediaPlayer) in delphi -
so far ive managed to get it to create controls that already come as standard, but I would like
it to be able to create controls by just supplying it with the string name of the control

As you can see, the code for it is already there, I just cant get it working! - the problems really
just getting it to find/use components when given the string name, and having spent many
a sleepless night on this, id really appreciate some help

Ive seen the code for this in a lot of MFC apps. - but they have an extended window handle
that allows components to be created quite easily.

Thanks for any help or tips

Heres the code

[code]

unit activeXBrowse;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, DB, Menus;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

function GetUniqueName(Component: TComponent): string;
var
intComponent: integer;
strClassName: string;
begin
Result:='';

strClassName:=Copy(Component.ClassName, 2, Length(Component.ClassName) - 1);
for intComponent:=1 to 1000 do
if Component.Owner.FindComponent(
strClassName+ IntToStr((intComponent))) = nil then
begin
Result:=strClassName+ IntToStr((intComponent));
Break;
end;
end;

function CreateControl(strComponentName: string; AOwner: TWinControl; X, Y: integer): TControl;
var
Component: TComponent;
AComponentClass: TComponentClass;
AClass: TPersistentClass;
begin
Result:=nil;
AClass:=GetClass(strComponentName);
Assert(Assigned(AClass), 'Class not registered ('+strComponentName+')');
AComponentClass:=TComponentClass(AClass);
Component:=(AComponentClass).Create(AOwner);
if Component is TControl then
begin
Result:=TControl(Component);
Result.Top :=Y;
Result.Left :=X;
Result.Parent:=AOwner;
Result.Name :=GetUniqueName(Result);
end
else
Component.Free;
end;

//From Borland.com
type TControlClass = class of TControl;

function CreateControl2(ControlClass: TControlClass;
const ControlName: string; X, Y, W, H: Integer): TControl;
begin
Result := ControlClass.Create(Form1);
with Result do
begin
Parent := Form1;
Name := ControlName;
SetBounds(X, Y, W, H);
Visible := True;
end;
end;

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
var
Wrapper: TControlClass;
AClass: TPersistentClass;
AComponentClass: TComponentClass;
Component: TComponent;
TT : TControl;


begin

//AClass:=GetClass('TEdit');
//Component:=(AComponentClass).Create(Form1);

//Component.FindComponent('TEdit');
//Wrapper.Create(Component);


CreateControl2(TEdit,'Edit1',10, 10, 100, 20);
//CreateControl2(Wrapper,'Edit1',10, 10, 100, 20);
//Wrapper := CreateControl('ShockwaveFlash.ShockwaveFlash',TWinControl(Sender),100,200);



end;

end.

[/code]

Comments

  • zibadianzibadian Posts: 6,349Member
    : Hi,
    : Im trying to get activeX controls to be created at runtime (for example MediaPlayer) in delphi -
    : so far ive managed to get it to create controls that already come as standard, but I would like
    : it to be able to create controls by just supplying it with the string name of the control
    :
    : As you can see, the code for it is already there, I just cant get it working! - the problems really
    : just getting it to find/use components when given the string name, and having spent many
    : a sleepless night on this, id really appreciate some help
    :
    : Ive seen the code for this in a lot of MFC apps. - but they have an extended window handle
    : that allows components to be created quite easily.
    :
    : Thanks for any help or tips
    :
    : Heres the code
    :
    : [code]
    :
    : unit activeXBrowse;
    :
    : interface
    :
    : uses
    : Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    : Dialogs, StdCtrls, ComCtrls, DB, Menus;
    :
    : type
    : TForm1 = class(TForm)
    : Button1: TButton;
    : procedure Button1Click(Sender: TObject);
    : private
    : { Private declarations }
    : public
    : { Public declarations }
    : end;
    :
    : var
    : Form1: TForm1;
    :
    : implementation
    :
    : function GetUniqueName(Component: TComponent): string;
    : var
    : intComponent: integer;
    : strClassName: string;
    : begin
    : Result:='';
    :
    : strClassName:=Copy(Component.ClassName, 2, Length(Component.ClassName) - 1);
    : for intComponent:=1 to 1000 do
    : if Component.Owner.FindComponent(
    : strClassName+ IntToStr((intComponent))) = nil then
    : begin
    : Result:=strClassName+ IntToStr((intComponent));
    : Break;
    : end;
    : end;
    :
    : function CreateControl(strComponentName: string; AOwner: TWinControl; X, Y: integer): TControl;
    : var
    : Component: TComponent;
    : AComponentClass: TComponentClass;
    : AClass: TPersistentClass;
    : begin
    : Result:=nil;
    : AClass:=GetClass(strComponentName);
    : Assert(Assigned(AClass), 'Class not registered ('+strComponentName+')');
    : AComponentClass:=TComponentClass(AClass);
    : Component:=(AComponentClass).Create(AOwner);
    : if Component is TControl then
    : begin
    : Result:=TControl(Component);
    : Result.Top :=Y;
    : Result.Left :=X;
    : Result.Parent:=AOwner;
    : Result.Name :=GetUniqueName(Result);
    : end
    : else
    : Component.Free;
    : end;
    :
    : //From Borland.com
    : type TControlClass = class of TControl;
    :
    : function CreateControl2(ControlClass: TControlClass;
    : const ControlName: string; X, Y, W, H: Integer): TControl;
    : begin
    : Result := ControlClass.Create(Form1);
    : with Result do
    : begin
    : Parent := Form1;
    : Name := ControlName;
    : SetBounds(X, Y, W, H);
    : Visible := True;
    : end;
    : end;
    :
    : {$R *.dfm}
    :
    :
    : procedure TForm1.Button1Click(Sender: TObject);
    : var
    : Wrapper: TControlClass;
    : AClass: TPersistentClass;
    : AComponentClass: TComponentClass;
    : Component: TComponent;
    : TT : TControl;
    :
    :
    : begin
    :
    : //AClass:=GetClass('TEdit');
    : //Component:=(AComponentClass).Create(Form1);
    :
    : //Component.FindComponent('TEdit');
    : //Wrapper.Create(Component);
    :
    :
    : CreateControl2(TEdit,'Edit1',10, 10, 100, 20);
    : //CreateControl2(Wrapper,'Edit1',10, 10, 100, 20);
    : //Wrapper := CreateControl('ShockwaveFlash.ShockwaveFlash',TWinControl(Sender),100,200);
    :
    :
    :
    : end;
    :
    : end.
    :
    : [/code]
    :
    For this to work you first need to find the GUID of the activeX control you want to use. This can be found in the registry under HKEY_CLASSES_ROOT. Each type may have a CLSID, which leads to the comserver details. This CLSID can be used in the CreateOleObject() function to create an actual instance of the interface.
    After this you need show the control using the interface. This is very difficult to do without typecasting the interface, due to the nature of COM.
    It is much simpler to create an TOleContainer as wrapper and build the document in there.
  • legacyelegacye Posts: 42Member
    Does anyone know if some working sample code on the topic?
Sign In or Register to comment.