(*
功能:IniFormState
1 保存和读取 窗体的大小
2
保存和读取 窗体在屏幕上的位置
//让程序第1次出现在屏幕的中心点
获取屏幕的中心点
POSITION:poScreenCenter
//不行
*)
//implementation
//{$R *.dfm}
uses IniFiles;//引用IniFiles
/// 写入信息到ini文件
procedure WriteformState(section:string;fileName:string;form:TForm);
var INIFILE: TIniFile;
begin
INIFILE:=TIniFile.Create(ExtractFilePath(Application.ExeName)+fileName);
try
INIFILE.WriteInteger(section,'Left', form.Left);
INIFILE.WriteInteger(section,'Top', form.Top);
INIFILE.WriteInteger(section,'Height',form.Height);
INIFILE.WriteInteger(section,'width', form.Width);
Finally
INIFILE.Free;
end;
end;
/// 从Ini读取信息并赋值
procedure ReadformState(section:string;fileName:string;form:TForm);
var
INIFILE: TIniFile;
frm1Left,frm1Top:integer;
begin
//FORM1 处于屏幕中心点时的left Top的值 用于第1次运行本程序时程序出现 在屏幕中心
frm1Left:=(Screen.Width-FORM.Width) div 2;
frm1Top :=(Screen.Height-FORM.Height) div 2;
INIFILE :=TIniFile.Create(ExtractFilePath(Application.ExeName)+fileName);
try
form.Left:= INIFILE.ReadInteger(section,'Left', frm1Left);
form.Top:= INIFILE.ReadInteger(section,'Top', frm1Top);
form.Height:= INIFILE.ReadInteger(section,'Height',form.Height);
form.Width:= INIFILE.ReadInteger(section,'Width', form.Width);
Finally
INIFILE.Free;
end;
end;
//调用
//读取信息
ReadformState('MainForm','Config.ini',Form1);
//写入信息
WriteformState('MainForm','Config.ini',Form1);