从剪贴板getComponent的问题(100分)

  • 主题发起人 主题发起人 qingxi
  • 开始时间 开始时间
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.

 
这么快就刷新到第二页了!??
help me,ok?
 
没有高手了吗?
 
我自己写了复制与粘贴的代码,没有采用以上方式。
不过我想你们是如何解决的?
 
unit2
//...
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;
procedure Add(lbl: TDeskNom);
destructor Destroy; override;
published
end;

implementation

procedure TmyPanel.Add(lbl: TDeskNom);
begin
if not assigned(lbl) then exit;
fno:=lbl;
Fno.parent:=self;
Fno.Align:=alTop;
end;

constructor TMypaneL.Create(Aowner:TComponent);
begin
inherited Create(Aowner);
end;


Destructor Tmypanel.Destroy;
begin
fno.free;
inherited Destroy;
end;
constructor TDeskNom.Create(Aowner:TComponent);
begin
inherited Create(Aowner);
caption:='this is one!!!';
end;

////////////////////////////
unit1
//...
implementation

uses Unit2;

{$R *.dfm}
var
k1:TmyPanel;
d1:TDeskNom;
procedure TForm1.Button1Click(Sender: TObject);
begin

k1:=TMypanel.create(self);
d1:=TDeskNom.Create(k1);
k1.Add(d1);
k1.parent:=self;
k1.visible:=true;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Clipboard.SetComponent(k1);
k1.Name :='savenight';
k1.Left:=40;

end;

procedure TForm1.Button3Click(Sender: TObject);
begin
ClipBoard.GetComponent(self,self);

end;

initialization
RegisterClasses([TmyPanel]);
RegisterClasses([TDeskNom]);

end.
 
后退
顶部