//unit SaveIni; TSaveStat类
//功能:实现保存一个TCustomControl中所有TCustomEdit类及子类的text
// 和TCheckBox类和子类的Checked 到Ini文件
//使用:在窗口创建时从ini文件读取设置
// 在窗口销毁时保存设置到Ini文件
//
//注意:一行文本字段最大是2050字节,所以文本的保存太长会有损失
//
//詹益辉 2005-6-1编写
//最后修改时间:2005-6-2
unit SaveIni;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, IniFiles;
const
RE_ENTER = #13#10;
RE_ENTER2 = #13;
//替换的字符
RE_ENTER_REPLAY = #11;
RE_ENTER_REPLAY2 = #12;
type
TSaveStat = class(TObject)
private
_Form: TControl;
_IniFileName: string;
//procedure SaveToInt(Form:TCustomControl);
protected
procedure StrReplace(var s: string; substr1: string; substr2: string);
public
constructor Create(Form: TControl; iniFilename: string = 'Confg.ini');
overload;
destructor Destroy; override;
published
end;
implementation
{ TSaveStat }
//-----------------------------------------------------------------------
procedure TSaveStat.StrReplace(var s: string; substr1: string; substr2: string);
var
i: Integer;
ts: string;
begin
ts := s;
s := '';
i := 1;
while True do
begin
if Copy(ts, i, length(substr1)) = substr1 then
begin
s := s + substr2;
i := i + length(substr1)
end
else
begin
s := s + Copy(ts, i, 1);
i := i + 1;
end;
if i > length(ts) then break;
end;
end;
//-----------------------------------------------------------------------
constructor TSaveStat.Create(Form: TControl; iniFilename: string = 'Confg.ini');
var
F: TINIfile;
i: integer;
TempStr: string;
begin
_Form := Form;
_IniFileName := iniFilename;
if pos(':/', _IniFileName) <= 0 then
begin
_IniFileName := ExtractFilePath(Application.ExeName) + _IniFileName;
end;
F := TINIFile.Create(_IniFileName);
try
for i := 0 to _Form.ComponentCount - 1 do
begin
if (_Form.Components[i] is TCustomEdit) then
begin
TempStr := F.ReadString(TCustomEdit(_Form.Components[i]).ClassName
, TCustomEdit(_Form.Components[i]).Name
, TCustomEdit(_Form.Components[i]).Text);
//还原原文本中的回车和换行
StrReplace(TempStr, RE_ENTER_REPLAY, RE_ENTER);
StrReplace(TempStr, RE_ENTER_REPLAY2, RE_ENTER2);
TCustomEdit(_Form.Components[i]).Text := TempStr;
end
else
if _Form.Components[i] is TCheckBox then
begin
TCheckBox(_Form.Components[i]).Checked :=
F.ReadBool(TCheckBox(_Form.Components[i]).ClassName
, TCheckBox(_Form.Components[i]).Name
, TCheckBox(_Form.Components[i]).Checked);
end;
end; //end for
finally
F.free;
end;
end;
//-----------------------------------------------------------------------
destructor TSaveStat.Destroy;
var
F: TINIfile;
i: integer;
TempStr: string;
begin
F := TINIFile.Create(_IniFileName);
try
for i := 0 to _Form.ComponentCount - 1 do
begin
if _Form.Components[i] is TCustomEdit then
begin
//替换文本中的回车和换行
TempStr := TCustomEdit(_Form.Components[i]).Text;
StrReplace(TempStr, RE_ENTER, RE_ENTER_REPLAY);
StrReplace(TempStr, RE_ENTER2, RE_ENTER_REPLAY2);
F.WriteString(TCustomEdit(_Form.Components[i]).ClassName
, TCustomEdit(_Form.Components[i]).Name
, TempStr);
end
else
if _Form.Components[i] is TCheckBox then
begin
F.WriteBool(TCheckBox(_Form.Components[i]).ClassName
, TCheckBox(_Form.Components[i]).Name
, TCheckBox(_Form.Components[i]).Checked);
end;
end; //end for
finally
F.free;
end;
end;
//-----------------------------------------------------------------------
end.
使用示例
saveMan: TSaveStat;
procedure TForm1.FormCreate(Sender: TObject);
begin
saveMan := TSaveStat.Create(Self,
ExpandFileName(Application.ExeName) + '.ini');
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
saveMan.Free;
end;
实际只这个类不只可以保存窗口上
你也可以选择只保存一个容器里的
比如只保存一个panel里的控件状态
saveMan := TSaveStat.Create(panel1,'config.ini');