D
dafei_ying
Unregistered / Unconfirmed
GUEST, unregistred user!
从TCollectionItem继承一个类,如TMyPersistent,在TMyPersistent类定义数字、文字、图像等属性,用TCollection类管理TMyPersistent。把TCollection管理下的TMyPersistent各属性值存储在一个自定义文件格式中,存储后的文件为空?
TMyPersistent:
unit store;
interface
uses
SysUtils, Classes, Controls,StdCtrls, Buttons,Graphics, ExtCtrls,Contnrs,Dialogs;
type
TMyPersistent = class(TCollectionItem)
private
FID,FName,FKind,FRemark:String;
FButtonLayer:TSpeedButton;
function GetButtonLayer: TSpeedButton;
procedure SetButtonLayer(const Value: TSpeedButton);
function GetID: String;
function GetName: String;
procedure SetID(const Value: String);
procedure SetName(const Value: String);
function GetRemark: String;
procedure SetRemark(const Value: String);
function GetKind: String;
procedure SetKind(const Value: String);
protected
procedure StoreLayerButttonProperty(Writer: TWriter);
procedure LoadLayerButttonProperty(Reader: TReader);
procedure StoreIDProperty(Writer: TWriter);
procedure LoadIDProperty(Reader: TReader);
procedure StoreNameProperty(Writer: TWriter);
procedure LoadNameProperty(Reader: TReader);
procedure StoreRemarkPerperty(Writer:TWriter);
procedure LoadRemarkPerperty(Reader:TReader);
procedure StoreKindPerperty(Writer:TWriter);
procedure LoadKindPerperty(Reader:TReader);
published
property ButtonLayer:TSpeedButton read GetButtonLayer write SetButtonLayer;
property ID:String read GetID write SetID;
property Name:String read GetName write SetName;
property Remark:String read GetRemark write SetRemark;
property Kind:String read GetKind write SetKind;
public
constructor Create(Collection : TCollection); override;
destructor Destroy; override;
procedure DefineProperties(Filer: TFiler); override;
end;
implementation
{ TMyPersistent }
constructor TMyPersistent.Create(Collection : TCollection);
begin
inherited Create(Collection);
FButtonLayer := TSpeedButton.Create(nil);
end;
procedure TMyPersistent.DefineProperties(Filer: TFiler);
begin
inherited;
Filer.DefineProperty('MyButtonLayerProperty', LoadLayerButttonProperty,StoreLayerButttonProperty, True);
Filer.DefineProperty('MyButtonLayerIDProperty', LoadIDProperty,StoreIDProperty, True);
Filer.DefineProperty('MyButtonLayerNameProperty', LoadNameProperty,StoreNameProperty, True);
Filer.DefineProperty('MyButtonLayerKindProperty', LoadKindPerperty,StoreKindPerperty, True);
Filer.DefineProperty('MyButtonLayerRemarkProperty', LoadRemarkPerperty,StoreRemarkPerperty, True);
end;
destructor TMyPersistent.Destroy;
begin
FButtonLayer.Free;
inherited;
end;
function TMyPersistent.GetButtonLayer: TSpeedButton;
begin
result := FButtonLayer;
end;
function TMyPersistent.GetID: String;
begin
Result := FID;
end;
function TMyPersistent.GetKind: String;
begin
Result := FKind;
end;
function TMyPersistent.GetName: String;
begin
Result := FName;
end;
function TMyPersistent.GetRemark: String;
begin
Result := FRemark;
end;
procedure TMyPersistent.LoadIDProperty(Reader: TReader);
begin
if Reader.ReadBoolean then
FID := Reader.ReadString;
end;
procedure TMyPersistent.LoadKindPerperty(Reader: TReader);
begin
if Reader.ReadBoolean then
FKind := Reader.ReadString;
end;
procedure TMyPersistent.LoadLayerButttonProperty(Reader: TReader);
var
acmp : TComponent;
begin
if Reader.ReadBoolean then
begin
RegisterClass(TSpeedButton);
acmp := Reader.ReadComponent(nil);
FButtonLayer := TSpeedButton(acmp);
end;
end;
procedure TMyPersistent.LoadNameProperty(Reader: TReader);
begin
if Reader.ReadBoolean then
FName := Reader.ReadString;
end;
procedure TMyPersistent.LoadRemarkPerperty(Reader: TReader);
begin
if Reader.ReadBoolean then
FRemark := Reader.ReadString;
end;
procedure TMyPersistent.SetButtonLayer(const Value: TSpeedButton);
begin
FButtonLayer := Value;
end;
procedure TMyPersistent.SetID(const Value: String);
begin
FID := Value;
end;
procedure TMyPersistent.SetKind(const Value: String);
begin
FKind := Value;
end;
procedure TMyPersistent.SetName(const Value: String);
begin
FName := Value;
end;
procedure TMyPersistent.SetRemark(const Value: String);
begin
FRemark := Value;
end;
procedure TMyPersistent.StoreIDProperty(Writer: TWriter);
begin
Writer.WriteBoolean(FID <> '');
if FID <> '' then
Writer.WriteString(FID);
end;
procedure TMyPersistent.StoreKindPerperty(Writer: TWriter);
begin
Writer.WriteBoolean(FKind <> '');
if FKind <> '' then
Writer.WriteString(FKind);
end;
procedure TMyPersistent.StoreLayerButttonProperty(Writer: TWriter);
begin
Writer.WriteBoolean(FButtonLayer <> nil);
if (FButtonLayer <> nil) then
Writer.WriteComponent(FButtonLayer);
end;
procedure TMyPersistent.StoreNameProperty(Writer: TWriter);
begin
Writer.WriteBoolean(FName <> '');
if FName <> '' then
Writer.WriteString(FName);
// Writer.WriteCollection();
end;
procedure TMyPersistent.StoreRemarkPerperty(Writer: TWriter);
begin
Writer.WriteBoolean(FRemark <> '');
if FRemark <> '' then
Writer.WriteString(FRemark);
end;
end.
窗体中:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, store, Buttons, ExtCtrls;
type
TForm1 = class(TForm)
BtnSave1: TButton;
BtnLoad: TButton;
Panel1: TPanel;
procedure FormCreate(Sender: TObject);
procedure BtnSave1Click(Sender: TObject);
procedure BtnLoadClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
FMemoryStream:TMemoryStream;
FListButton:TList;
FStart:TPoint;
FCollection:TCollection;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
PERFILENAME = 'test.cks';
procedure TForm1.FormCreate(Sender: TObject);
begin
FMemoryStream := TMemoryStream.Create;
FCollection := TCollection.Create(TMyPersistent);
end;
procedure TForm1.BtnSave1Click(Sender: TObject);
var
lButton:TSpeedButton;
po:TMyPersistent;
i:integer;
MyFileSize:integer;
Writer: TWriter;
begin
Writer := TWriter.Create(FMemoryStream,2048);
lButton := TSpeedButton.Create(self);
lButton.Parent := Panel1;
lButton.Left := 39;
lButton.Top := 55 ;
lButton.Glyph.LoadFromFile('a.bmp');
po := (FCollection.add) as TMyPersistent;
po.ID := '1';
po.Name := 'test' ;
po.ButtonLayer := lButton;
Writer.WriteCollection(FCollection);
FMemoryStream.Position := 0;
FMemoryStream.SaveToFile(PERFILENAME); //保存后的文件为空
Writer.Free;
end;
procedure TForm1.BtnLoadClick(Sender: TObject);
begin
//
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FMemoryStream.Free;
FCollection.Free;
end;
end.
TMyPersistent:
unit store;
interface
uses
SysUtils, Classes, Controls,StdCtrls, Buttons,Graphics, ExtCtrls,Contnrs,Dialogs;
type
TMyPersistent = class(TCollectionItem)
private
FID,FName,FKind,FRemark:String;
FButtonLayer:TSpeedButton;
function GetButtonLayer: TSpeedButton;
procedure SetButtonLayer(const Value: TSpeedButton);
function GetID: String;
function GetName: String;
procedure SetID(const Value: String);
procedure SetName(const Value: String);
function GetRemark: String;
procedure SetRemark(const Value: String);
function GetKind: String;
procedure SetKind(const Value: String);
protected
procedure StoreLayerButttonProperty(Writer: TWriter);
procedure LoadLayerButttonProperty(Reader: TReader);
procedure StoreIDProperty(Writer: TWriter);
procedure LoadIDProperty(Reader: TReader);
procedure StoreNameProperty(Writer: TWriter);
procedure LoadNameProperty(Reader: TReader);
procedure StoreRemarkPerperty(Writer:TWriter);
procedure LoadRemarkPerperty(Reader:TReader);
procedure StoreKindPerperty(Writer:TWriter);
procedure LoadKindPerperty(Reader:TReader);
published
property ButtonLayer:TSpeedButton read GetButtonLayer write SetButtonLayer;
property ID:String read GetID write SetID;
property Name:String read GetName write SetName;
property Remark:String read GetRemark write SetRemark;
property Kind:String read GetKind write SetKind;
public
constructor Create(Collection : TCollection); override;
destructor Destroy; override;
procedure DefineProperties(Filer: TFiler); override;
end;
implementation
{ TMyPersistent }
constructor TMyPersistent.Create(Collection : TCollection);
begin
inherited Create(Collection);
FButtonLayer := TSpeedButton.Create(nil);
end;
procedure TMyPersistent.DefineProperties(Filer: TFiler);
begin
inherited;
Filer.DefineProperty('MyButtonLayerProperty', LoadLayerButttonProperty,StoreLayerButttonProperty, True);
Filer.DefineProperty('MyButtonLayerIDProperty', LoadIDProperty,StoreIDProperty, True);
Filer.DefineProperty('MyButtonLayerNameProperty', LoadNameProperty,StoreNameProperty, True);
Filer.DefineProperty('MyButtonLayerKindProperty', LoadKindPerperty,StoreKindPerperty, True);
Filer.DefineProperty('MyButtonLayerRemarkProperty', LoadRemarkPerperty,StoreRemarkPerperty, True);
end;
destructor TMyPersistent.Destroy;
begin
FButtonLayer.Free;
inherited;
end;
function TMyPersistent.GetButtonLayer: TSpeedButton;
begin
result := FButtonLayer;
end;
function TMyPersistent.GetID: String;
begin
Result := FID;
end;
function TMyPersistent.GetKind: String;
begin
Result := FKind;
end;
function TMyPersistent.GetName: String;
begin
Result := FName;
end;
function TMyPersistent.GetRemark: String;
begin
Result := FRemark;
end;
procedure TMyPersistent.LoadIDProperty(Reader: TReader);
begin
if Reader.ReadBoolean then
FID := Reader.ReadString;
end;
procedure TMyPersistent.LoadKindPerperty(Reader: TReader);
begin
if Reader.ReadBoolean then
FKind := Reader.ReadString;
end;
procedure TMyPersistent.LoadLayerButttonProperty(Reader: TReader);
var
acmp : TComponent;
begin
if Reader.ReadBoolean then
begin
RegisterClass(TSpeedButton);
acmp := Reader.ReadComponent(nil);
FButtonLayer := TSpeedButton(acmp);
end;
end;
procedure TMyPersistent.LoadNameProperty(Reader: TReader);
begin
if Reader.ReadBoolean then
FName := Reader.ReadString;
end;
procedure TMyPersistent.LoadRemarkPerperty(Reader: TReader);
begin
if Reader.ReadBoolean then
FRemark := Reader.ReadString;
end;
procedure TMyPersistent.SetButtonLayer(const Value: TSpeedButton);
begin
FButtonLayer := Value;
end;
procedure TMyPersistent.SetID(const Value: String);
begin
FID := Value;
end;
procedure TMyPersistent.SetKind(const Value: String);
begin
FKind := Value;
end;
procedure TMyPersistent.SetName(const Value: String);
begin
FName := Value;
end;
procedure TMyPersistent.SetRemark(const Value: String);
begin
FRemark := Value;
end;
procedure TMyPersistent.StoreIDProperty(Writer: TWriter);
begin
Writer.WriteBoolean(FID <> '');
if FID <> '' then
Writer.WriteString(FID);
end;
procedure TMyPersistent.StoreKindPerperty(Writer: TWriter);
begin
Writer.WriteBoolean(FKind <> '');
if FKind <> '' then
Writer.WriteString(FKind);
end;
procedure TMyPersistent.StoreLayerButttonProperty(Writer: TWriter);
begin
Writer.WriteBoolean(FButtonLayer <> nil);
if (FButtonLayer <> nil) then
Writer.WriteComponent(FButtonLayer);
end;
procedure TMyPersistent.StoreNameProperty(Writer: TWriter);
begin
Writer.WriteBoolean(FName <> '');
if FName <> '' then
Writer.WriteString(FName);
// Writer.WriteCollection();
end;
procedure TMyPersistent.StoreRemarkPerperty(Writer: TWriter);
begin
Writer.WriteBoolean(FRemark <> '');
if FRemark <> '' then
Writer.WriteString(FRemark);
end;
end.
窗体中:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, store, Buttons, ExtCtrls;
type
TForm1 = class(TForm)
BtnSave1: TButton;
BtnLoad: TButton;
Panel1: TPanel;
procedure FormCreate(Sender: TObject);
procedure BtnSave1Click(Sender: TObject);
procedure BtnLoadClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
FMemoryStream:TMemoryStream;
FListButton:TList;
FStart:TPoint;
FCollection:TCollection;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
PERFILENAME = 'test.cks';
procedure TForm1.FormCreate(Sender: TObject);
begin
FMemoryStream := TMemoryStream.Create;
FCollection := TCollection.Create(TMyPersistent);
end;
procedure TForm1.BtnSave1Click(Sender: TObject);
var
lButton:TSpeedButton;
po:TMyPersistent;
i:integer;
MyFileSize:integer;
Writer: TWriter;
begin
Writer := TWriter.Create(FMemoryStream,2048);
lButton := TSpeedButton.Create(self);
lButton.Parent := Panel1;
lButton.Left := 39;
lButton.Top := 55 ;
lButton.Glyph.LoadFromFile('a.bmp');
po := (FCollection.add) as TMyPersistent;
po.ID := '1';
po.Name := 'test' ;
po.ButtonLayer := lButton;
Writer.WriteCollection(FCollection);
FMemoryStream.Position := 0;
FMemoryStream.SaveToFile(PERFILENAME); //保存后的文件为空
Writer.Free;
end;
procedure TForm1.BtnLoadClick(Sender: TObject);
begin
//
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FMemoryStream.Free;
FCollection.Free;
end;
end.