请教根据字符串的类型名,动态创建TWinControl派生出的子类的方法 ( 积分: 100 )

  • 主题发起人 主题发起人 大花鸡
  • 开始时间 开始时间

大花鸡

Unregistered / Unconfirmed
GUEST, unregistred user!
type
TParents = class(TWinControl)
public
constructor create(AOwner: TComponent)
override;
destructor destroy
override;
procedure add(S: String)
virtual
abstract;
end;

TChild1 = class(TParents)
private
SS: String;
public
constructor create(AOwner: TComponent)
override;
destructor destroy
override;

procedure add(S: String)
override;
end;

TChild2 = class(TParents)
private
SS: String;
public
constructor create(AOwner: TComponent)
override;
destructor destroy
override;
procedure add(S: String)
override;
end;
。。。。。。
{ TParents }

constructor TParents.create(AOwner: TComponent);
begin
inherited;
end;

destructor TParents.destroy;
begin
inherited;
end;

{ TChild2 }

procedure TChild2.add(S: String);
begin
inherited;
showmessage('TChild2.add ' + S);
if Self.Owner = nil then
showmessage('NIL')
else
showmessage(Owner.Name);
end;

constructor TChild2.create(AOwner: TComponent);
begin
inherited;
ss := 'asfd';
end;

destructor TChild2.destroy;
begin
inherited;
end;

{ TChild1 }

procedure TChild1.add(S: String);
begin
inherited;
showmessage('TChild2.add ' + S);
if Self.Owner = nil then
showmessage('NIL')
else
showmessage(Owner.Name);
end;

constructor TChild1.create(AOwner: TComponent);
begin
inherited;
ss := 'asfd';
end;

destructor TChild1.destroy;
begin
inherited;
end;

initialization
RegisterClasses([TChild2, TChild1]);
finalization
UnRegisterClass(TChild2);
UnRegisterClass(TChild1);

使用:
procedure TForm1.Button1Click(Sender: TObject);

var
s: String;
cc: TParents;
begin
s := 'TChild' + Edit2.Text;
CC :=(FindClass(S).Create as TParents);
CC.Parent := form1;
CC.add(Edit1.Text);
CC.Free;
end;

这样是可以创建TChildX的实例,可是没有handle,也没法设Parent。怎么 解决这个问题?
 
在Button1的Click中这样调用
if FindClass(s) <> nil then
TParents(FindClass(s)).Create(Self);
出现内存违便访问,晕啊
 
unit Unit1;

interface

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

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


TParents = class(TWinControl)
public
constructor create(AOwner: TComponent)
override;
destructor destroy
override;
procedure add(S: String)
virtual
abstract;
end;

TTparents = class of TParents;

TChild1 = class(TParents)
private
SS: String;
public
constructor create(AOwner: TComponent)
override;
destructor destroy
override;

procedure add(S: String)
override;
end;

TChild2 = class(TParents)
private
SS: String;
public
constructor create(AOwner: TComponent)
override;
destructor destroy
override;
procedure add(S: String)
override;
end;


var
Form1: TForm1;

implementation

{$R *.dfm}

constructor TParents.create(AOwner: TComponent);
begin
inherited;
end;

destructor TParents.destroy;
begin
inherited;
end;

{ TChild2 }

procedure TChild2.add(S: String);
begin
inherited;
showmessage('TChild2.add ' + S);
if Self.Owner = nil then
showmessage('NIL')
else
showmessage(Owner.Name);
end;

constructor TChild2.create(AOwner: TComponent);
begin
inherited;
ss := 'asfd';
end;

destructor TChild2.destroy;
begin
inherited;
end;

{ TChild1 }

procedure TChild1.add(S: String);
begin
inherited;
showmessage('TChild2.add ' + S);
if Self.Owner = nil then
showmessage('NIL')
else
showmessage(Owner.Name);
end;

constructor TChild1.create(AOwner: TComponent);
begin
inherited;
ss := 'asfd';
end;

destructor TChild1.destroy;
begin
inherited;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
s: String;
cc: TTparents;
c: Tparents;
begin
s := 'TChild' + Edit2.Text;
CC := TTparents(FindClass(S)) ;
c := CC.create(form1);
c.Parent := form1;
c.add(Edit1.Text);
c.Free;


end;

initialization
RegisterClasses([TChild2, TChild1]);
finalization
UnRegisterClass(TChild2);
UnRegisterClass(TChild1);

end.
 
改成能用的了,具体你要干什么不清楚,只是根据语义帮你改了一下语法
 
谢谢,已经可以用了。呵呵
我主要是想用TChildX的Handle,现在可以有Handle了[:D]
 
后退
顶部