Delphi组件保存

Delphi组件保存,delphi,object,save,delphi-7,record,Delphi,Object,Save,Delphi 7,Record,如何最好地保存此组件和所有内部变量?代码示例将不胜感激 TSmall = record fName: string[30]; fAge: integer; fID_Number: string[30]; end; TRec = record ArraySmall: array[1..10] of TSmall; end; TBigComponent = class(TComponent) private fS

如何最好地保存此组件和所有内部变量?代码示例将不胜感激

  TSmall = record
      fName: string[30];
      fAge: integer;
      fID_Number: string[30];
  end;

  TRec = record
    ArraySmall: array[1..10] of TSmall;
  end;

  TBigComponent = class(TComponent)
    private
      fSmallArr: TRec;
      fCompCount: integer;
      fBigName: string;
    public
      procedure AddNew(Name: string; Age: integer; ID: string);
      procedure Save(FileName: string);
      procedure Load(FileName: string);
      procedure SetName(Name: string);
      function GetName: string;
      function ToString: string;
    published
      property SmallArr: TRec read fSmallArr write fSmallArr;
      property Count: integer read fCompCount write fCompCount;
      property Name: string read fBigName write fBigName;
    end;

要使用Delphi内部持久性和RTTI,您应该使用类而不是记录

这里有很多好的建议和例子:


如果您正在寻找将自定义数据保存到可视化组件的示例,请检查文件ComCtrls.pas中的方法TTreeNodes.DefineProperties的Delphi VCL源。

要使用Delphi内部持久性和RTTI,应使用类而不是记录

这里有很多好的建议和例子:


如果您正在寻找将自定义数据保存到可视化组件的示例,请检查文件ComCtrls.pas中方法TTreeNodes.DefineProperties的Delphi VCL源。

通常,遵循VilleK的建议并利用Tpersistent提供的内容可能是最简单的

但是,我更喜欢这种方法,所以我完全控制文件的结构

type
  TFileStruct=packed record
    fSmallArr: TRec;
    fCompCount: UINT32; // be explicit.. who knows what 64bit Delphi does to your integers...
    fBigName: String[250]; // AnsiChar
  end;

procedure TBigComponent.Save(FileName: string);
var
  F:File of TFileStruct;
  FileStruct:TFileStruct;
begin
  FileStruct.fSmallArr := fSmallArr;
  FileStruct.fCompCount := fCompCount;
  FileStruct.fBigName := fBigName;

  AssignFile(F,FileName);
  Rewrite(F);
  Write(F,FileStruct);
  CloseFile(F);
end;
请记住,字符串[xxx]似乎被视为AnsiString,因此如果使用Delphi 2009,保存时Unicode字符串将被更改为AnsiString。至少可以与使用旧版本的Delphi编译的软件交换文件

在TSmall中,我会将Age的整数改为Byte,这样您就不会在64位Delphi中遇到麻烦了。

“8位应该对每个人都足够了”(c)2009 Wouter:-)

一般来说,遵循VilleK的建议并利用Tpersistent提供的可能是最容易的

但是,我更喜欢这种方法,所以我完全控制文件的结构

type
  TFileStruct=packed record
    fSmallArr: TRec;
    fCompCount: UINT32; // be explicit.. who knows what 64bit Delphi does to your integers...
    fBigName: String[250]; // AnsiChar
  end;

procedure TBigComponent.Save(FileName: string);
var
  F:File of TFileStruct;
  FileStruct:TFileStruct;
begin
  FileStruct.fSmallArr := fSmallArr;
  FileStruct.fCompCount := fCompCount;
  FileStruct.fBigName := fBigName;

  AssignFile(F,FileName);
  Rewrite(F);
  Write(F,FileStruct);
  CloseFile(F);
end;
请记住,字符串[xxx]似乎被视为AnsiString,因此如果使用Delphi 2009,保存时Unicode字符串将被更改为AnsiString。至少可以与使用旧版本的Delphi编译的软件交换文件

在TSmall中,我会将Age的整数改为Byte,这样您就不会在64位Delphi中遇到麻烦了。

“8位应该对每个人都足够了”(c)2009 Wouter:-)

对Wouter的建议做了一点小小的改进:

 type
  TSmall = record
      fName: string[30];
      fAge: integer;
      fID_Number: string[30];
  end;

  TRec = record
    ArraySmall: array[1..10] of TSmall;
  end;

  TBigComponent = class(TComponent)
  private
      type
        TInternalFields = record
          SmallArr: TRec;
          CompCount: integer;
          BigName: Shortstring;
       end;
    var
      FFields : TInternalFields;
    public
      procedure AddNew(Name: string; Age: integer; ID: string);
      procedure Save(FileName: string);
      procedure Load(FileName: string);
      procedure SetName(Name: string);reintroduce;
      function GetName: string;
      function ToString: string;
    published
      property SmallArr: TRec read FFields.SmallArr write FFields.SmallArr;
      property Count: integer read FFields.CompCount write FFields.CompCount;
      property Name: ShortString read FFields.BigName write FFields.BigName;
    end;

procedure TBigComponent.Save(FileName: string);
var
  F:File of TInternalFields;
begin
  AssignFile(F,FileName);
  Rewrite(F);
  Write(F, FFields);
  CloseFile(F);
end;
这样就不需要将对象中的每个字段复制到记录中,因为它已经存在于记录中


我不确定是什么时候添加了read Record.field语法的-这是2006年对Wouter建议的一个小小的改进:

 type
  TSmall = record
      fName: string[30];
      fAge: integer;
      fID_Number: string[30];
  end;

  TRec = record
    ArraySmall: array[1..10] of TSmall;
  end;

  TBigComponent = class(TComponent)
  private
      type
        TInternalFields = record
          SmallArr: TRec;
          CompCount: integer;
          BigName: Shortstring;
       end;
    var
      FFields : TInternalFields;
    public
      procedure AddNew(Name: string; Age: integer; ID: string);
      procedure Save(FileName: string);
      procedure Load(FileName: string);
      procedure SetName(Name: string);reintroduce;
      function GetName: string;
      function ToString: string;
    published
      property SmallArr: TRec read FFields.SmallArr write FFields.SmallArr;
      property Count: integer read FFields.CompCount write FFields.CompCount;
      property Name: ShortString read FFields.BigName write FFields.BigName;
    end;

procedure TBigComponent.Save(FileName: string);
var
  F:File of TInternalFields;
begin
  AssignFile(F,FileName);
  Rewrite(F);
  Write(F, FFields);
  CloseFile(F);
end;
这样就不需要将对象中的每个字段复制到记录中,因为它已经存在于记录中


我不确定何时添加了read Record.field语法-它是在2006年(非主题)根据David I Integer将保持32位(非主题)根据David的说法,整数将保持32位谢谢你指出-我不知道read Record.field语法。谢谢你指出-我不知道read Record.field语法。