PDA

View Full Version : serialize



idehrayan
شنبه 18 بهمن 1393, 21:11 عصر
سلام
بزرگواران لطفا کمک کنید
این یونیت مربوط به serialize (http://delphi.about.com/od/windowsshellapi/a/reader-writer.htm)کردن فرم هستش (ذخیره کردن فرم) اگه ۲ تا فرم داشته باشیم form1 و form2 و بخواهیم از طریق form۱ فرم ۲ رو ذخیره و بازیابی کنیم. چطور میتونیم از این یونیت استفاده کنیم؟


unit SettingsU;
interface
uses Classes;
{$M+}
type
TCustomSettings = class
public
procedure LoadFromStream(const Stream: TStream) ;
procedure LoadFromFile(const FileName: string) ;
procedure SaveToStream(const Stream: TStream) ;
procedure SaveToFile(const FileName: string) ;
end;
TSettings = class(TCustomSettings)
private
FPropertyString: string;
FPropertyDate: TDateTime;
FPropertyInt: Integer;
published
property PropertyInt: Integer read FPropertyInt write FPropertyInt;
property PropertyString: string read FPropertyString write FPropertyString;
property PropertyDate: TDateTime read FPropertyDate write FPropertyDate;
end;
var
Settings: TSettings;
implementation
uses TypInfo, Sysutils;
{ TSettings }
procedure TCustomSettings.LoadFromFile(const FileName: string) ;
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite) ;
try
LoadFromStream(Stream) ;
finally
Stream.Free;
end;
end;
procedure TCustomSettings.LoadFromStream(const Stream: TStream) ;
var
Reader: TReader;
PropName, PropValue: string;
begin
Reader := TReader.Create(Stream, $FFF) ;
Stream.Position := 0;
Reader.ReadListBegin;
while not Reader.EndOfList do
begin
PropName := Reader.ReadString;
PropValue := Reader.ReadString;
SetPropValue(Self, PropName, PropValue) ;
end;
FreeAndNil(Reader) ;
end;
procedure TCustomSettings.SaveToFile(const FileName: string) ;
var
Stream: TStream;
begin
Stream := TFileStream.Create(FileName, fmCreate) ;
try
SaveToStream(Stream) ;
finally
Stream.Free;
end;
end;
procedure TCustomSettings.SaveToStream(const Stream: TStream) ;
var
PropName, PropValue: string;
cnt: Integer;
lPropInfo: PPropInfo;
lPropCount: Integer;
lPropList: PPropList;
lPropType: PPTypeInfo;
Writer: TWriter;
begin
lPropCount := GetPropList(PTypeInfo(ClassInfo), lPropList) ;
Writer := TWriter.Create(Stream, $FFF) ;
Stream.Size := 0;
Writer.WriteListBegin;
for cnt := 0 to lPropCount - 1 do
begin
lPropInfo := lPropList^[cnt];
lPropType := lPropInfo^.PropType;
if lPropType^.Kind = tkMethod then Continue;
PropName := lPropInfo.Name;
PropValue := GetPropValue(Self, lPropInfo) ;
Writer.WriteString(PropName) ;
Writer.WriteString(PropValue) ;
end;
Writer.WriteListEnd;
FreeAndNil(Writer) ;
end;
initialization
Settings := TSettings.Create;
finalization
FreeAndNil(Settings) ;
end.
Using the TSettings class is easy:
uses SettingsU;
...
begin
Settings.PropertyInt := 1;
Settings.PropertyString := 'String';
Settings.PropertyDate := Now;
ShowMessage('Settings.Property2 : ' + Settings.PropertyString) ;
//save the object Settings.SaveToFile('Settings.dmp') ;
Settings.PropertyInt := 2;
Settings.PropertyString := 'String2';
Settings.PropertyDate := Now + 200;
ShowMessage('Settings.Property2 : ' + Settings.PropertyString) ;
//now load saved state Settings.LoadFromFile('Settings.dmp') ;
ShowMessage('Settings.Property2 : ' + Settings.PropertyString) ;

end;