Mega Code Archive

 
Categories / Delphi / VCL
 

Streaming Components

Title: Streaming Components Question: 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 cant 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.