Figure 1 - Creating a default TPersistent field
{interface}
type DemoComponent =
class(TComponent)
private
fGlyph: TBitmap;
fGlyphWritten: boolean;
procedure SetGlyph(Glyph: TBitmap); {not shown}
protected
constructor Create(Owner: TComponent); override;
procedure Loaded; override;
public
published
property Glyph: TBitmap read fGlyph write SetGlyph;
end;
{implementation}
constructor DemoComponent.Create(Owner: TComponent);
begin
inherited Create(Owner);
fGlyph := TBitmap.Create;
{Be sure to fill in the field with an empty object}
end;
procedure DemoComponent.SetGlyph(Glyph: TBitmap);
begin
if fGlyph <> Glyph then { fGlyph = Glyph when SetGlyph is }
begin { called by the Loaded procedure }
fGlyph.Free; {Assign can fail if the target is not Empty:}
fGlyph := TBitmap.Create; {Free/Create/Assign is a lot safer}
fGlyph.Assign(Glyph);
end;
{Extract any necessary data ... set the PropertyWritten flag}
fGlyphWritten := True;
end;
procedure DemoComponent.Loaded;
begin
inherited Loaded; {Don’t forget to do this!}
if (not fGlyphWritten) and (not fGlyph.Empty) then
SetGlyph(fGlyph); {Extract any necessary data from the bitmap}
end;
Back to the article
Copyright © 1995,
Jon Shemitz -
jon@midnightbeach.com -
HTML markup 2-10-96