如果連同子控件一起復制到剪切板, 需要定義一個新類型.
譬如在一個 TPanel 中包含一個 TEdit; 在復制 TPanel 時, 若要連同 TEdit 一起復制, 需要重新從 TPanel 中繼承出一個類來(譬如是 TMyPanel), 把 TEdit 包含在新的類中.
運行效果圖:
TMyPanel 類的單元:
unit MyPanel;
interface
uses Classes, StdCtrls, ExtCtrls;
type
TMyPanel = class(TPanel)
Edit1: TEdit;
constructor Create(AOwner: TComponent); override;
end;
implementation
{ TMyPanel }
constructor TMyPanel.Create(AOwner: TComponent);
begin
inherited;
Edit1 := TEdit.Create(Self);
Edit1.Parent := Self;
Edit1.Left := 10;
Edit1.Top := 10;
RegisterClasses([TMyPanel]); {在這裡就給注冊了}
end;
end.
測試單元:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses Clipbrd, MyPanel;
var
obj: TComponent;
pnl: TMyPanel;
procedure TForm1.FormCreate(Sender: TObject);
begin
pnl := TMyPanel.Create(Self);
pnl.Parent := Self;
pnl.Edit1.Text := '一起被復制';
Button1.Caption := '復制';
Button2.Caption := '粘貼';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Clipboard.SetComponent(pnl);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if Clipboard.HasFormat(CF_COMPONENT) then
begin
obj := Clipboard.GetComponent(Self, Self);
TMypanel(obj).Left := 20;
TMypanel(obj).Top := 60;
end;
end;
end.