To extract the 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 the component from the 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 of an Edit;
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Text := ComponentToStringProc(Edit1);
end;
To create Edit from the obtained DFM:
procedure TForm1.Button7Click(Sender: TObject);
var
EdtX: TEdit;
begin
EdtX := StringToComponentProc(Memo1.Text) as TEdit;
EdtX.Parent := Form1;
end;
In initialization register the TEdit class or the components you want.
initialization
RegisterClass(TEdit);
Source: link