Author: Jonas Bilinkevicius
I would like to stream selected components (say a panel with some other components
on it) to a DFM like (text preferred over binary) file and be able to load it and
restore them programatically.
Answer:
You need to save this component with root (where its handlers are declared), f.e.,
with TFormX:
1 { ... }2 type3 TForm1 = class(TForm)
4 MyComponent: TMyComponent;
5 procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
6 private7 { Private declarations }8 public9 constructor Create(AOwner: TComponent); override;
10 function FormFileName: string;
11 { Public declarations }12 end;
13 14 var15 Form1: TForm1;
16 17 implementation18 19 {$R *.DFM}20 21 function TForm1.FormFilename: string;
22 begin23 Result := ExtractFilePath(ParamStr(0)) + Classname + '.DAT';
24 end;
25 26 constructor TForm1.Create(AOwner: TComponent);
27 var28 fname: string;
29 begin30 RegisterClass(TMyComponent); {And anything else that is required }31 fname := FormFilename;
32 if FileExists(fname) then33 begin34 CreateNew(AOwner);
35 ReadComponentResFile(fname, Self);
36 end37 else38 inherited Create(AOwner);
39 end;
40 41 procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
42 begin43 WriteComponentResFile(FormFileName, Self);
44 UnregisterClass(TMyComponent);
45 end;