我给你一个简单的例子吧,从书上摘来的。
DELPHIPT.PAS
unit DelphiPt;
interface
uses
Classes, Graphics;
type
TDDHPoint = class (TCollectionItem)
private
fX, fY: Integer;
public
Text: string;
procedure WriteText (Writer: TWriter);
procedure ReadText (Reader: TReader);
procedure DefineProperties (Filer: TFiler);
override;
procedure Paint (Canvas: TCanvas);
procedure Assign (Pt: TPersistent);
override;
published
property X: Integer read fX write fX;
property Y: Integer read fY write fY;
end;
TWrapper = class (TComponent)
private
FColl: TCollection;
published
property MyColl: TCollection read FColl write FColl;
public
constructor Create (Owner: TComponent);
override;
destructor Destroy;
override;
end;
implementation
// TWrapper constructor and destructor
constructor TWrapper.Create (Owner: TComponent);
begin
inherited Create (Owner);
FColl := TCollection.Create (TDDHPoint);
end;
destructor TWrapper.Destroy;
begin
FColl.Clear;
FColl.Free;
inherited Destroy;
end;
// class TDDHPoint methods
procedure TDDHPoint.WriteText (Writer: TWriter);
begin
Writer.WriteString (Text);
end;
procedure TDDHPoint.ReadText (Reader: TReader);
begin
Text := Reader.ReadString;
end;
procedure TDDHPoint.DefineProperties (Filer: TFiler);
begin
Filer.DefineProperty (
'Text', ReadText, WriteText, (Text <> ''));
end;
procedure TDDHPoint.Paint (Canvas: TCanvas);
begin
Canvas.Ellipse (fX - 3, fY - 3, fX + 3, fY + 3);
Canvas.TextOut (fX + 5, fY + 5, Text);
end;
procedure TDDHPoint.Assign (Pt: TPersistent);
begin
if Pt is TDDHPoint then
begin
fx := TDDHPoint (Pt).fX;
fY := TDDHPoint (Pt).fY;
Text := TDDHPoint (Pt).Text;
end
else
// raise an exception
inherited Assign (pt);
end;
//initialization
//RegisterClass (TWrapper);
end.
PERSFORM.DFM
object Form1: TForm1
Left = 295
Top = 161
Width = 462
Height = 300
Caption = 'Persist'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OnCreate = FormCreate
OnDestroy = FormDestroy
OnMouseDown = FormMouseDown
OnPaint = FormPaint
PixelsPerInch = 96
TextHeight = 13
object Panel1: TPanel
Left = 0
Top = 0
Width = 454
Height = 39
Align = alTop
ParentShowHint = False
ShowHint = False
TabOrder = 0
object Label1: TLabel
Left = 8
Top = 12
Width = 106
Height = 13
Caption = 'Enter name then
click:'
end
object SpeedButtonLoad: TSpeedButton
Left = 256
Top = 8
Width = 49
Height = 25
Caption = 'Load...'
NumGlyphs = 2
OnClick = SpeedButtonLoadClick
end
object SpeedButtonSave: TSpeedButton
Left = 304
Top = 8
Width = 49
Height = 25
Caption = 'Save...'
NumGlyphs = 2
OnClick = SpeedButtonSaveClick
end
object Edit1: TEdit
Left = 120
Top = 8
Width = 121
Height = 21
TabOrder = 0
Text = 'Point 1'
end
end
object OpenDialog1: TOpenDialog
DefaultExt = 'prs'
Filter = 'Persistent File (*.prs)|*.prs|Any file (*.*)|*.*'
Options = [ofHideReadOnly, ofPathMustExist, ofFileMustExist]
Left = 32
Top = 64
end
object SaveDialog1: TSaveDialog
DefaultExt = 'prs'
Filter = 'Persistent File (*.prs)|*.prs|Any file (*.*)|*.*'
Options = [ofHideReadOnly, ofPathMustExist, ofCreatePrompt]
Left = 112
Top = 65
end
end
PERSFORM.PAS
unit PersForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Buttons, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Edit1: TEdit;
SpeedButtonLoad: TSpeedButton;
SpeedButtonSave: TSpeedButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
procedure FormCreate(Sender: TObject);
procedure SpeedButtonSaveClick(Sender: TObject);
procedure SpeedButtonLoadClick(Sender: TObject);
procedure FormMouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
procedure FormDestroy(Sender: TObject);
procedure FormPaint(Sender: TObject);
private
PtList: TCollection;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
uses
DelphiPt;
procedure TForm1.FormCreate(Sender: TObject);
begin
PtList := TCollection.Create (TDDHPoint);
end;
procedure TForm1.SpeedButtonSaveClick(Sender: TObject);
var
Str1: TFileStream;
Wrap: TWrapper;
begin
if SaveDialog1.Execute then
begin
Str1 := TFileStream.Create (SaveDialog1.FileName,
fmOpenWrite or fmCreate);
try
Wrap := TWrapper.Create (self);
try
Wrap.MyColl.Assign (ptList);
Str1.WriteComponent (Wrap);
finally
Wrap.Free;
end;
finally
Str1.Free;
end;
end;
end;
procedure TForm1.SpeedButtonLoadClick(Sender: TObject);
var
Str1: TFileStream;
Wrap: TWrapper;
begin
if OpenDialog1.Execute then
begin
Str1 := TFileStream.Create (
OpenDialog1.Filename, fmOpenRead);
try
Wrap := TWrapper.Create (self);
try
Wrap := Str1.ReadComponent (Wrap) as TWrapper;
ptList.Assign (Wrap.MyColl);
finally
Wrap.Free;
end;
finally
Str1.Free;
Invalidate;
Edit1.Text := 'Point ' + IntToStr (PtList.Count + 1);
end;
end;
end;
procedure TForm1.FormMouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
var
Pt: TDDHPoint;
begin
Pt := PtList.Add as TDDHPoint;
Pt.X := X;
Pt.Y := Y;
Pt.Text := Edit1.Text;
Edit1.Text := 'Point ' + IntToStr (PtList.Count + 1);
Invalidate;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
// empty and destroy the list
PtList.Clear;
PtList.Free;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
I: Integer;
begin
for I := 0 to PtList.Count - 1do
TDDHPoint (PtList.Items ).Paint (Canvas);
end;
end.
PERSIST.DPR
program Persist;
uses
Forms,
PersForm in 'PersForm.pas' {Form1},
DelphiPt in 'DelphiPt.pas';
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
--------------------------------------------------------------------------------