Q
qingxi
Unregistered / Unconfirmed
GUEST, unregistred user!
动态创建一个Panel,在它的Create中再以它为Owner和panel创建了一个TLabel,
然后将此控件使用Clipboard.setComponent(Panel)放到剪贴板上,
最后使用CLipBoard.getComponent(Panel)取回时,发现上面有两个TLABel!!!
它又调用了一次panel.create方法。
分析Clipbrd单元,发现其GetComponent方法用了insertComponent,
可不可以避免?请各位指点!!!
源程序如下:
//===========================组件单元===============================
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls,stdctrls,Clipbrd;
type
TDeskNom=Class(TLabel)
private
protected
public
constructor Create(AOwner: TComponent); override;
published
end;
TmyPanel=class(TPanel)
private
fno:TDeskNOm;
protected
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
end;
implementation
constructor TMypaneL.Create(Aowner:TComponent);
begin
inherited Create(Aowner);
Fno:=TDesknom.Create(self);
Fno.parent:=self;
Fno.Align:=alTop;
end;
Destructor Tmypanel.Destroy;
begin
fno.free;
inherited Destroy;
end;
constructor TDeskNom.Create(Aowner:TComponent);
begin
inherited Create(Aowner);
caption:='this is one!!!';
end;
end.
//================主窗口====================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Clipbrd,StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2;
{$R *.dfm}
var
k1:TmyPanel;
procedure TForm1.Button1Click(Sender: TObject);
begin
k1:=TMypanel.create(self);
k1.parent:=self;
k1.visible:=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Clipboard.SetComponent(k1);
k1.Left:=40;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ClipBoard.GetComponent(self,self);
end;
initialization
RegisterClasses([TmyPanel]);
RegisterClasses([TDeskNom]);
end.
然后将此控件使用Clipboard.setComponent(Panel)放到剪贴板上,
最后使用CLipBoard.getComponent(Panel)取回时,发现上面有两个TLABel!!!
它又调用了一次panel.create方法。
分析Clipbrd单元,发现其GetComponent方法用了insertComponent,
可不可以避免?请各位指点!!!
源程序如下:
//===========================组件单元===============================
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls,stdctrls,Clipbrd;
type
TDeskNom=Class(TLabel)
private
protected
public
constructor Create(AOwner: TComponent); override;
published
end;
TmyPanel=class(TPanel)
private
fno:TDeskNOm;
protected
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
end;
implementation
constructor TMypaneL.Create(Aowner:TComponent);
begin
inherited Create(Aowner);
Fno:=TDesknom.Create(self);
Fno.parent:=self;
Fno.Align:=alTop;
end;
Destructor Tmypanel.Destroy;
begin
fno.free;
inherited Destroy;
end;
constructor TDeskNom.Create(Aowner:TComponent);
begin
inherited Create(Aowner);
caption:='this is one!!!';
end;
end.
//================主窗口====================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Clipbrd,StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2;
{$R *.dfm}
var
k1:TmyPanel;
procedure TForm1.Button1Click(Sender: TObject);
begin
k1:=TMypanel.create(self);
k1.parent:=self;
k1.visible:=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Clipboard.SetComponent(k1);
k1.Left:=40;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ClipBoard.GetComponent(self,self);
end;
initialization
RegisterClasses([TmyPanel]);
RegisterClasses([TDeskNom]);
end.