怎样将控件的所有属性保存到一个文件或者字段中,需要时再根据文件或者字段恢复该控件的属性?

解决方案 »

  1.   

    对象持久化可以达到你的要求
    将控件写入文件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;//这样,在设计期(运行期)控件是怎样的,那保存后再读取回来就是怎样的。。
      

  2.   

    第一段代码少了句 : FS.Free;
      

  3.   


    //看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;
      

  4.   

    ReadComponentResFile是重新创建了一个对象.我要的时把对象的属性存储起来,需要时再恢复它的属性,并不是要再新建一个对象.
      

  5.   

    慢慢看inside vcl吧
    另外这种控件也有的是,比如jedi vcl的TJvFormStorage,也可以参考一下