[BlueLeaf1336]> PROGRAM>

フォームの状態を記録する

historyTOP

2003/08/31:作成

overviewTOP

フォームの内容を記録するようなクラスです。方法は、Inifileに書き込んでおくだけです。
TEdit.Text や TCheckBox.Checked など、アプリケーションの設定画面なんかで大量に使うことになって保存が嫌になりがちな設定内容を記録します。

downloadTOP

iForm.zip(1,728bytes):ソース

codeTOP

(*
    #######################################################

    目的    :   フォームの内容を保存・復元する
    注意    :   Form.OnShow で Load
                Form.OnClose で Save
    履歴    :   2003/08/31 作成

    記録内容:   TEdit.Text
                TCheckBox.Checked
                TRadioButton.Checked
                TRadioGroup.ItemIndex
                TPageControl.ActivePageIndex
                TTabControl.TabIndex
                TTrackBar.Position


    復元例1 :   procedure TForm1.FormShow(Sender: TObject);
                begin
                    LoadForm(Self);
                end;

    保存例1 :   procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
                begin
                    SaveForm(Self);
                end;

    #######################################################
*)

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

            // TEdit.Text
            if (Compo is TEdit) then
                with TEdit(Compo) do
                    SaveIniString(Name, Text)

            // TCheckBox.Checked
            else if (Compo is TCheckBox) then
                with TCheckBox(Compo) do
                    SaveIniBoolean(Name, Checked)

            // TRadioButton.Checked
            else if (Compo is TRadioButton) then
                with TRadioButton(Compo) do
                    SaveIniBoolean(Name, Checked)

            // TRadioGroup.ItemIndex
            else if (Compo is TRadioGroup) then
                with TRadioGroup(Compo) do
                    SaveIniInteger(Name, ItemIndex)

            // TPageControl.ActivePageIndex
            else if (Compo is TPageControl) then
                with TPageControl(Compo) do
                    SaveIniInteger(Name, ActivePageIndex)

            // TTabControl.TabIndex
            else if (Compo is TTabControl) then
                with TTabControl(Compo) do
                    SaveIniInteger(Name, TabIndex)

            // TTrackBar.Position
            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

            // TEdit.Text
            if (Compo is TEdit) then
                with TEdit(Compo) do
                    Text := LoadIniString(Name, Text)

            // TCheckBox.Checked
            else if (Compo is TCheckBox) then
                with TCheckBox(Compo) do
                    Checked := LoadIniBoolean(Name, Checked)

            // TRadioButton.Checked
            else if (Compo is TRadioButton) then
                with TRadioButton(Compo) do
                    Checked := LoadIniBoolean(Name, Checked)

            // TRadioGroup.ItemIndex
            else if (Compo is TRadioGroup) then
                with TRadioGroup(Compo) do
                    ItemIndex := LoadIniInteger(Name, ItemIndex)

            // TPageControl.ActivePageIndex
            else if (Compo is TPageControl) then
                with TPageControl(Compo) do
                    ActivePageIndex := LoadIniInteger(Name, ActivePageIndex)

            // TTabControl.TabIndex
            else if (Compo is TTabControl) then
                with TTabControl(Compo) do
                    TabIndex := LoadIniInteger(Name, TabIndex)

            // TTrackBar.Position
            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.

EOFTOP