对象持久化可以达到你的要求 将控件写入文件var FS : TFileStream; i : integer; begin Fs := TFilestream.Create(FileName,fmcreate or fmopernwrite); For i := 0 to controlcount - 1 do beign fs.writecomponent(controls[i] as tcomponent); end; end;//读取控件 var FS : TFileStream; FComponent : TComponent; begin Fs := TFilestream.Create(FileName,fmopenRead); while Fs.Position < FS.Size do begin FComponent := fs.readcomponent(nil); InsertControl(Fcomponent as tcontrol); FComponent.parent := form1; end; FS.Free; end;//这样,在设计期(运行期)控件是怎样的,那保存后再读取回来就是怎样的。。
第一段代码少了句 : FS.Free;
//看Delphi WriteComponentResFile帮助 unit StrmExmpl; {$R-,T-,H+,X+} interface uses Classes type TContainedClass = class(TPersistent) private: FSomeData: Integer; procedure SetSomeData(Value: Integer); public: constructor Create; override; // Only published properties // are automatically streamed. published: property SomeData: Integer read FSomeData write SetSomeData;end; TStreamableClass = class(TComponent) private: FContainedClass: TContainedClass; public: constructor Create(AOwner: TComponent); override; destructor Destroy; override; // Only published properties // are automatically streamed. published: property ContainedClass: TContainedClass read FContainedClass write FContainedClass;end;implementationprocedure TContainedClass.SetSomeData(Value: Integer); begin { Place a breakpoint here and notice how the data is streamed back. } FSomeData := Value; end; constructor TContainedClass.Create; begin FSomeData := 42; end; constructor TStreamableClass.Create(AOwner: TComponent); begin inherited Create(AOwner); FContainedClass = TContainedClass.Create;end; destructor TStreamableClass.Destroy; begin FContainedClass.Free; end; initialization RegisterClasses([TContainedClass, TStreamableClass]); finalization end.The following OnClick event handler should be added to a button on the application’s main form. It causes the above classes to be streamed out and in. Note that you must add StrmExmpl to the uses clause of unit1.procedure TForm1.Button1Click(Sender: TObject);var AClassInstance: TStreamableClass; begin AClassInstance := TStreamableClass.Create(nil); WriteComponentResFile('C:\Test', AClassInstance); FreeAndNil(AClassInstance); AClassInstance := ReadComponentResFile('C:\Test', nil) as TStreamableClass; FreeAndNil(AClassInstance); end;
将控件写入文件var FS : TFileStream;
i : integer;
begin
Fs := TFilestream.Create(FileName,fmcreate or fmopernwrite);
For i := 0 to controlcount - 1 do
beign
fs.writecomponent(controls[i] as tcomponent);
end;
end;//读取控件
var FS : TFileStream;
FComponent : TComponent;
begin
Fs := TFilestream.Create(FileName,fmopenRead);
while Fs.Position < FS.Size do
begin
FComponent := fs.readcomponent(nil);
InsertControl(Fcomponent as tcontrol);
FComponent.parent := form1;
end;
FS.Free;
end;//这样,在设计期(运行期)控件是怎样的,那保存后再读取回来就是怎样的。。
//看Delphi WriteComponentResFile帮助
unit StrmExmpl;
{$R-,T-,H+,X+}
interface
uses Classes
type
TContainedClass = class(TPersistent)
private:
FSomeData: Integer; procedure SetSomeData(Value: Integer); public: constructor Create; override; // Only published properties
// are automatically streamed.
published: property SomeData: Integer read FSomeData write SetSomeData;end;
TStreamableClass = class(TComponent)
private:
FContainedClass: TContainedClass;
public: constructor Create(AOwner: TComponent); override; destructor Destroy; override; // Only published properties
// are automatically streamed.
published: property ContainedClass: TContainedClass read FContainedClass write FContainedClass;end;implementationprocedure TContainedClass.SetSomeData(Value: Integer);
begin
{ Place a breakpoint here and notice how the data is streamed back. }
FSomeData := Value;
end;
constructor TContainedClass.Create;
begin
FSomeData := 42;
end;
constructor TStreamableClass.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FContainedClass = TContainedClass.Create;end;
destructor TStreamableClass.Destroy;
begin
FContainedClass.Free;
end;
initialization
RegisterClasses([TContainedClass, TStreamableClass]);
finalization
end.The following OnClick event handler should be added to a button on the application’s main form. It causes the above classes to be streamed out and in. Note that you must add StrmExmpl to the uses clause of unit1.procedure TForm1.Button1Click(Sender: TObject);var
AClassInstance: TStreamableClass;
begin
AClassInstance := TStreamableClass.Create(nil);
WriteComponentResFile('C:\Test', AClassInstance);
FreeAndNil(AClassInstance); AClassInstance := ReadComponentResFile('C:\Test', nil) as TStreamableClass;
FreeAndNil(AClassInstance);
end;
另外这种控件也有的是,比如jedi vcl的TJvFormStorage,也可以参考一下