unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
{* Die zu kopierende Klasse }
TMyClass =
class (TButton)
public
constructor Create (AOwner: TComponent);
override;
end;
type
TForm1 =
class(TForm)
Button1 : TButton;
procedure Button1Click (Sender: TObject);
procedure FormCreate (Sender: TObject);
private
MyButton : TMyClass;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
//==============================================================================
constructor TMyClass. Create (AOwner: TComponent);
begin
inherited Create (AOwner);
Parent:= TWinControl (AOwner);
Left := 10;
Top := 100;
Width := 100;
Caption:= '
Test Copy';
end;
//==============================================================================
// Die Kopierfunktion
//==============================================================================
function Copy (AOwner: TComponent; ASource: TMyClass): TMyClass;
type
TCopyClass =
class of TMyClass;
var
CopyClass : TCopyClass;
begin
CopyClass := TCopyClass (ASource. ClassType);
Result := TMyClass (CopyClass. Create (AOwner));
end;
procedure TForm1. Button1Click (Sender: TObject);
var
NewButton : TMyClass;
begin
{* nu kopier mal }
NewButton := Copy (self, MyButton);
NewButton. Left := MyButton. Left + MyButton. Width + 10;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MyButton:= TMyClass. Create (self);
end;
end.