WriteComponent,ReadComponent
WriteComponent,ReadComponent
代码
var
Form1: TForm1;
ms: TMemoryStream;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
aBtn: TButton;
begin
aBtn := TButton.Create(Self);
aBtn.Parent := Self;
aBtn.Caption := 'Test';
aBtn.Left := 10;
ms.WriteComponent(aBtn);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
abtn: TButton;
begin
try
Classes.FindClass('TButton')
except
Classes.RegisterClass(TButton);
end;
abtn := TButton(ms.ReadComponent(nil));
aBtn.Parent := Self;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ms := TMemoryStream.Create;
if FileExists('btn.data') then
ms.LoadFromFile('btn.data');
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
ms.SaveToFile('btn.data');
FreeAndNil(ms);
end;