unit Form.BaseForm;
interface
uses
Services,
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs;
type
TBaseForm =
class( TForm )
private
FLoaded: Boolean;
procedure InternalLoadSettings( Service: IFormSettingsService );
procedure InternalStoreSettings( Service: IFormSettingsService );
protected
function GetBlockName:
string;
virtual;
procedure DoClose(
var Action: TCloseAction );
override;
procedure DoShow;
override;
procedure DoLoadSettings( Settings: TBinaryReader );
virtual;
procedure DoStoreSettings( Settings: TBinaryWriter );
virtual;
public
procedure LoadSettings;
procedure StoreSettings;
end;
var
BaseForm: TBaseForm;
implementation
{$R *.dfm}
uses
FMX.
Platform;
{ TBaseForm }
procedure TBaseForm.DoClose(
var Action: TCloseAction );
var
LService: IFormSettingsService;
begin
inherited;
end;
procedure TBaseForm.DoLoadSettings( Settings: TBinaryReader );
begin
end;
procedure TBaseForm.DoShow;
begin
if not FLoaded
then
LoadSettings;
FLoaded := True;
inherited;
end;
procedure TBaseForm.DoStoreSettings( Settings: TBinaryWriter );
begin
end;
function TBaseForm.GetBlockName:
string;
begin
Result := Self.ClassName;
end;
procedure TBaseForm.InternalLoadSettings( Service: IFormSettingsService );
var
LBlockData: TMemoryStream;
LReader: TBinaryReader;
begin
LBlockData := TMemoryStream.Create;
try
if Service.GetBlock( GetBlockName, LBlockData )
then
begin
LBlockData.Seek( 0, soFromBeginning );
LReader := TBinaryReader.Create( LBlockData );
try
DoLoadSettings( LReader );
finally
LReader.Free;
end;
end;
finally
LBlockData.Free;
end;
end;
procedure TBaseForm.InternalStoreSettings( Service: IFormSettingsService );
var
LBlockData: TMemoryStream;
LWriter: TBinaryWriter;
begin
LBlockData := TMemoryStream.Create;
try
LWriter := TBinaryWriter.Create( LBlockData );
try
DoStoreSettings( LWriter );
finally
LWriter.Free;
end;
LBlockData.Seek( 0, soFromBeginning );
Service.SetBlock( GetBlockName, LBlockData );
finally
LBlockData.Free;
end;
end;
procedure TBaseForm.LoadSettings;
var
LService: IFormSettingsService;
begin
if TPlatformServices.Current.SupportsPlatformService( IFormSettingsService, LService )
then
InternalLoadSettings( LService );
end;
procedure TBaseForm.StoreSettings;
var
LService: IFormSettingsService;
begin
if TPlatformServices.Current.SupportsPlatformService( IFormSettingsService, LService )
then
InternalStoreSettings( LService );
end;
end.