2006. szeptember 17., vasárnap
Streaming Components
Problem/Question/Abstract:
Saving and loading Component Published Propertys to disk
Answer:
The TStreams Class has a nice feature Couse it can stream component propertys in just one line .
So here's the complete unit where i Stream the component
If your class is not in the same unit you will have to register it .
{
procedure RegisterClasses(AClasses: array of TPersistentClass);
Description
Call RegisterClasses to register a set of custom classes in a single line. Each class is registered by calling RegisterClass. Unregistered classes can’t be loaded or saved by the VCL streaming system.
}
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
// jus a enumeration
TSomeints = 1..10;
// set of the enumeration
TExampleSet = set of TSomeints;
TExampleComponent = class(TComponent)
private
FASet: TExampleSet;
FAString: string;
FAFloat: Double;
FAInteger: Integer;
published
// Anyting you want streamed and is streameble by Delphi you can publish
// as property
property AString: string read FAString write FAString;
property ASet: TExampleSet read FASet write FASet;
property AInteger: Integer read FAInteger write FAInteger;
property AFloat: Double read FAFloat write FAFloat;
end;
TForm1 = class(TForm)
SaveToStream: TButton;
LoadFromStream: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
EAFloat: TEdit;
EAstring: TEdit;
procedure SaveToStreamClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure LoadFromStreamClick(Sender: TObject);
private
FExampleComponent: TExampleComponent;
procedure SetExampleComponent(const Value: TExampleComponent);
{ Private declarations }
public
property ExampleComponent: TExampleComponent read FExampleComponent write
SetExampleComponent;
procedure ObjectToGui;
procedure GuiToObject;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.SaveToStreamClick(Sender: TObject);
var
AStream: TMemoryStream;
begin
if SaveDialog1.execute then
begin
GuiToObject;
AStream := TMemoryStream.Create;
try
AStream.WriteComponent(ExampleComponent);
AStream.SaveToFile(SaveDialog1.FileName);
finally
AStream.free;
end;
end;
end;
procedure TForm1.SetExampleComponent(const Value: TExampleComponent);
begin
FExampleComponent := Value;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// this is the Procedure To Call if you want to Use components not defined
// in this unit ;
// RegisterClasses([TButton,TMemo,TEnz]);
FExampleComponent := TExampleComponent.Create(Self);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FExampleComponent.free;
end;
procedure TForm1.LoadFromStreamClick(Sender: TObject);
var
AStream: TMemoryStream;
begin
if OpenDialog1.execute then
begin
AStream := TMemoryStream.Create;
try
AStream.LoadFromFile(OpenDialog1.FileName);
AStream.ReadComponent(ExampleComponent);
ObjectToGui;
finally
AStream.free;
end;
end;
end;
procedure TForm1.GuiToObject;
begin
ExampleComponent.AString := EAstring.Text;
ExampleComponent.AFloat := StrToFloat(EAFloat.Text);
end;
procedure TForm1.ObjectToGui;
begin
EAstring.Text := ExampleComponent.AString;
EAFloat.Text := FloatToStr(ExampleComponent.AFloat);
end;
end.
Component Download: http://www.xs4all.nl/~suusie/Pieter/Programs/StreamingComponent.zip
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése