[delphi技术]实现保存窗口上的所有Edit和checkbox状态的类
来源:互联网 发布:网络电视机哪个牌子好 编辑:程序博客网 时间:2024/05/23 21:42
//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');
- [delphi技术]实现保存窗口上的所有Edit和checkbox状态的类
- delphi 把窗体上的所有EDIT清空
- 保存GridView checkbox 选中的状态(Session)
- easyui 分页保存checkbox的选中状态
- 一行代码实现 所有的Checkbox全选和反选
- DockPanel2 保存各个窗口的状态
- Edit控件密码窗口的秘密--通用密码记录技术
- 实现TreeView状态的保存
- delphi实现保存和读取图片的方法
- 关于GridView翻页checkBox状态保存的问题
- 关于GridView翻页checkBox状态保存的问题 (session)
- 关于GridView翻页checkBox状态保存的问题
- 关于GridView翻页checkBox状态保存的问题
- 保存Repeater控件中input CheckBox的选中状态
- easyui 分页 保存checkbox的 选中状态,操作选中记录。
- 关于CheckBox状态保存并回显的例子
- listview中的checkbox状态保存与恢复的完美方法
- Delphi 如何实现窗口透明,但是窗口上的文字不透明
- 自定义函数 将十进制转化为16进制,将16换成 36的话,就可以转化为所有拼音字母的进制
- 异常处理经验谈
- ASP网站漏洞解析及黑客入侵防范方法 选择自 hmilyld 的 Blog
- TEdit只能输入数据
- 老片复习手记·1
- [delphi技术]实现保存窗口上的所有Edit和checkbox状态的类
- SQLServer和Oracle常用函数对比 选择自 AppleBBS 的 Blog
- 在SQL中删除重复记录(多种方法) 选择自 55015182 的 Blog
- [delphi技术]常用的几个字符串处理函数
- 一种处理stack blown异常的简单方法
- ATL7窗口类剖析
- [delphi技术]文件搜索等几个文件处理函数
- 玩家们,不要再沉迷!
- 有时候可能就是坚持+时间