|
Saving and loading the form and components
TFormDesigner allows to save and load the entire form and each of component on the form in text or binary format. The format of the saved data is fully compatible with standard DFM file and with VCL stream formats. The entire form can be saved in text or binary DFM format by the SaveToDFM procedure.
FormDesigner1.SaveToDFM('theform.dfm',dfmText);
The first parameter is file name and the second parameter is data format, that can be dfmText or dfmBinary. The procedre LoadFormDFM loads the form from the file.
FormDesigner1.LoadFromDFM('theform.dfm',dfmText);
The events of the form and components are saved but they can be loaded correctly if the form contains the compatible events with the same names in the published section, when some event is not declared the event is ignored automatically when loading.
If you want to keep all current controls on th eform and just to add the controls from the DFM file, set the ClearBeforeLoad property to False (this property is True by default).
Each component on the form can be saved to stream and loaded from stream using the SaveComponentToStream and LoadComponentFromStream routines.
var
S: TFileStream;
...
S:=TFileStream.Create('component.dat',fmOpenWrite);
try
FormDesigner1.SaveComponentToStream(S,GroupBox1,dfmText);
finally
S.Free;
end;
The LoadComponentFromStream function receives the Component parameter. If this parameter is nil the new component will be created when loading from stream, if you pass the existing component instead, the passed component will be modified. The function returns the loaded component in any case.
var
S: TFileStream;
NewGroupBox: TGroupBox;
...
S:=TFileStream.Create('component.dat',fmOpenWrite);
try
// creating the new component from stream
NewGroupBox:=TGroupBox(
FormDesigner1.LoadComponentFromStream(S,nil,dfmText));
// changing some property
NewGroupBox.Caption:='Another caption';
// resetting the stream
S.Seek(0,soFromBeginning);
// reverting to the stored properties
FormDesigner1.LoadComponentFromStream(S,NewGroupBox,dfmText);
finally
S.Free;
end;
Nothe that the loading control from stream does not insert the control into the form, so to see the control you have to insert it manually.
NewGroupBox:=TGroupBox(
FormDesigner1.LoadComponentFromStream(S,nil,dfmText));
NewGroupBox.Parent:=TheParentControlForYourNewGroupBox;
The VCL streaming system requires the pre-registeroing all the loaded classes before loading them from DFM files or streams, so call the standard RegisterClasses procedure for all used classes.
RegisterClasses([TButton,TLabel,TGroupBox]);
|