設定ファイルにウインドウ状態を保存するようなアプリケーションのためのコード。
unit Unit1;
interface
uses
SysUtils, Forms, IniFiles;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
const
IniFileName = 'app.ini';
procedure TForm1.FormCreate(Sender: TObject);
var
IniFile: TIniFile;
begin
IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + IniFileName);
with IniFile do
begin
Top := ReadInteger('Window', 'Top', Top);
Left := ReadInteger('Window', 'Left', Left);
Width := ReadInteger('Window', 'Width', Width);
Height := ReadInteger('Window', 'Height', Height);
WindowState := TWindowState(ReadInteger('Window', 'State', Ord(WindowState)));
end;
IniFile.Free;
end;
procedure TForm1.FormDestroy(Sender: TObject);
var
IniFile: TIniFile;
begin
IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + IniFileName);
with IniFile do
begin
if WindowState = wsNormal then
begin
WriteInteger('Window', 'Top', Top);
WriteInteger('Window', 'Left', Left);
WriteInteger('Window', 'Width', Width);
WriteInteger('Window', 'Height', Height);
end;
WriteInteger('Window', 'State', Ord(WindowState));
end;
IniFile.Free;
end;
end.