To extract DFM code from a component:
function ComponentToStringProc(Component: TComponent): string;
var
BinStream:TMemoryStream;
StrStream: TStringStream;
s: string;
begin
BinStream := TMemoryStream.Create;
try
StrStream := TStringStream.Create(s);
try
BinStream.WriteComponent(Component);
BinStream.Seek(0, soFromBeginning);
ObjectBinaryToText(BinStream, StrStream);
StrStream.Seek(0, soFromBeginning);
Result:= StrStream.DataString;
finally
StrStream.Free;
end;
finally
BinStream.Free
end;
end;
To create component from DFM code:
function StringToComponentProc(Value: string): TComponent;
var
StrStream:TStringStream;
BinStream: TMemoryStream;
begin
StrStream := TStringStream.Create(Value);
try
BinStream := TMemoryStream.Create;
try
ObjectTextToBinary(StrStream, BinStream);
BinStream.Seek(0, soFromBeginning);
Result:= BinStream.ReadComponent(nil);
finally
BinStream.Free;
end;
finally
StrStream.Free;
end;
end;
To use do the following:
Get the DFM code from an Edit;
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Text := ComponentToStringProc(Edit1);
end;
To create Edit from the downloaded DFM:
procedure TForm1.Button7Click(Sender: TObject);
var
EdtX: TEdit;
begin
EdtX := StringToComponentProc(Memo1.Text) as TEdit;
EdtX.Parent := Form1;
end;
No initialization register the Tedit class or components you want.
initialization
RegisterClass(TEdit);
Source: http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/ComponentToString_(Delphi)
As already answered by @Roberto, you need to create the class in hand and set the properties as desired. A solution to automate a little this process would be using the plugin Gexperts, have an option to right-click mouse and "Component to code", it already returns all the necessary code.
– Confundir