[BlueLeaf1336]>
PROGRAM>
フォームの状態を記録する
2003/08/31:作成
フォームの内容を記録するようなクラスです。方法は、Inifileに書き込んでおくだけです。
TEdit.Text や TCheckBox.Checked など、アプリケーションの設定画面なんかで大量に使うことになって保存が嫌になりがちな設定内容を記録します。
iForm.zip(1,728bytes):ソース
unit iForm;
interface
uses
Forms, IniFiles;
procedure LoadForm(AForm: TForm);
procedure SaveForm(AForm: TForm);
function AppIni(): TIniFile;
implementation
uses
SysUtils, Classes, ComCtrls, StdCtrls, ExtCtrls;
type
TRestore = class
private
IniFile : TIniFile;
SectionName : string;
procedure SaveIniString(AName, AValue: string);
function LoadIniString(AName: string; ADefault: string = 'Name'): string;
procedure SaveIniBoolean(AName: string; AValue: Boolean);
function LoadIniBoolean(AName: string; ADefault: Boolean = False): Boolean;
procedure SaveIniInteger(AName: string; AValue: integer);
function LoadIniInteger(AName: string; ADefault: integer = 0): integer;
public
constructor Create(AIniName: string);
destructor Destroy(); override;
procedure Save(AForm: TForm);
procedure Load(AForm: TForm);
end;
var
Restore: TRestore = nil;
IniName: string = 'Restore.ini';
procedure LoadForm(AForm: TForm);
begin
try
Restore.Load(AForm);
except
;
end;
end;
procedure SaveForm(AForm: TForm);
begin
try
Restore.Save(AForm);
except
;
end;
end;
function AppIni(): TIniFile;
begin
Result := Restore.IniFile;
end;
procedure TRestore.SaveIniString(AName, AValue: string);
begin
try
IniFile.WriteString(SectionName, AName, AValue);
except
;
end;
end;
function TRestore.LoadIniString(AName: string; ADefault: string = 'Name'): string;
begin
try
Result := IniFile.ReadString(SectionName, AName, ADefault);
except
Result := ADefault;
end;
end;
procedure TRestore.SaveIniBoolean(AName: string; AValue: Boolean);
begin
try
IniFile.WriteBool(SectionName, AName, AValue);
except
;
end;
end;
function TRestore.LoadIniBoolean(AName: string; ADefault: Boolean = False): Boolean;
begin
try
Result := IniFile.ReadBool(SectionName, AName, ADefault);
except
Result := ADefault;
end;
end;
procedure TRestore.SaveIniInteger(AName: string; AValue: integer);
begin
try
IniFile.WriteInteger(SectionName, AName, AValue);
except
;
end;
end;
function TRestore.LoadIniInteger(AName: string; ADefault: integer = 0): integer;
begin
try
Result := IniFile.ReadInteger(SectionName, AName, ADefault);
except
Result := ADefault;
end;
end;
constructor TRestore.Create(AIniName: string);
begin
try
IniFile := TIniFile.Create(AIniName);
except
IniFile := nil;
end;
end;
destructor TRestore.Destroy();
begin
IniFile.Free;
end;
procedure TRestore.Save(AForm: TForm);
var
i: integer;
Compo: TComponent;
begin
SectionName := AForm.Name;
with AForm do
begin
SaveIniInteger(SectionName + '_Top', Top);
SaveIniInteger(SectionName + '_Left', Left);
SaveIniInteger(SectionName + '_Width', Width);
SaveIniInteger(SectionName + '_Height', Height);
end;
for i := 0 to AForm.ComponentCount - 1 do
begin
Compo := AForm.Components[i];
try
if (Compo is TEdit) then
with TEdit(Compo) do
SaveIniString(Name, Text)
else if (Compo is TCheckBox) then
with TCheckBox(Compo) do
SaveIniBoolean(Name, Checked)
else if (Compo is TRadioButton) then
with TRadioButton(Compo) do
SaveIniBoolean(Name, Checked)
else if (Compo is TRadioGroup) then
with TRadioGroup(Compo) do
SaveIniInteger(Name, ItemIndex)
else if (Compo is TPageControl) then
with TPageControl(Compo) do
SaveIniInteger(Name, ActivePageIndex)
else if (Compo is TTabControl) then
with TTabControl(Compo) do
SaveIniInteger(Name, TabIndex)
else if (Compo is TTrackBar) then
with TTrackBar(Compo) do
SaveIniInteger(Name, Position)
else
;
except
;
end;
end;
end;
procedure TRestore.Load(AForm: TForm);
var
i: integer;
Compo: TComponent;
begin
SectionName := AForm.Name;
with AForm do
begin
Top := LoadIniInteger(SectionName + '_Top', Top);
Left := LoadIniInteger(SectionName + '_Left', Left);
Width := LoadIniInteger(SectionName + '_Width', Width);
Height := LoadIniInteger(SectionName + '_Height', Height);
end;
for i := 0 to AForm.ComponentCount - 1 do
begin
Compo := AForm.Components[i];
try
if (Compo is TEdit) then
with TEdit(Compo) do
Text := LoadIniString(Name, Text)
else if (Compo is TCheckBox) then
with TCheckBox(Compo) do
Checked := LoadIniBoolean(Name, Checked)
else if (Compo is TRadioButton) then
with TRadioButton(Compo) do
Checked := LoadIniBoolean(Name, Checked)
else if (Compo is TRadioGroup) then
with TRadioGroup(Compo) do
ItemIndex := LoadIniInteger(Name, ItemIndex)
else if (Compo is TPageControl) then
with TPageControl(Compo) do
ActivePageIndex := LoadIniInteger(Name, ActivePageIndex)
else if (Compo is TTabControl) then
with TTabControl(Compo) do
TabIndex := LoadIniInteger(Name, TabIndex)
else if (Compo is TTrackBar) then
with TTrackBar(Compo) do
Position := LoadIniInteger(Name, Position)
else
;
except
;
end;
end;
end;
initialization
IniName := ExtractFilePath(Application.ExeName) + IniName;
Restore := TRestore.Create(IniName);
finalization
Restore.Free;
end.