📄 052603_05.htm
字号:
<html>
<head>
<link REL="stylesheet" HREF="/style.css" TYPE="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>计算机世界日报:
Delphi应用程序中保存状态信息的两种方法
</title>
<!--***********-->
</head>
<body bgcolor="#FFFFFF" >
<p><br>
<font color="#b904e8"><b><em><a href="class.htm">返回</a></em></b></font>
<center>
<font color="#0000c0"><h2>
Delphi应用程序中保存状态信息的两种方法
</h2></font>
<h3>
成都市电子科技大学96<br>
范静俭
</h3>
</center>
<p><font color="#ffffff">----</font>
许多For Windows的应用程序在一次运行结束时会自动保存此次运行的状态信息,这样做的好处是用户只要修改一次程序的运行状态,在下次启动此程序时,该程序会以上次运行的状态出现,用户可以按照自己的习惯来调整应用程序。
<p><font color="#ffffff">----</font>
在Delphi中有两种方法可以实现这种功能:使用INI文件或使用注册表。下面将详细阐述这两种方法。
<p><font color="#ffffff">----</font>
<b>一、 使用INI文件保存状态信息</b>
<p><font color="#ffffff">----</font>
Delphi中提供了TIniFile类,TIniFile类能够在INI文件中存取应用程序专用信息,我们可以用它来处理INI文件。注意使用时将IniFiles单元加入uses列表中。
<p><font color="#ffffff">----</font>
使用INI文件存取应用程序专用信息是Windows 3.x中的标准方法。INI文件中的信息储存是分段的,每个段由一个括在方括号中的名称说明,比如WIN.INI中就包括[Desktop]等段。每个段中又由一些键值组成,这些键值的格式是:< keyname >=< value >,共有三种类型:字符串、整型和布尔型,每种类型有两种方法可以读写:ReadBool,WriteBool,ReadInteger,WriteInteger,ReadString,WriteString。下面就一个例子来说明它们的用法:
<p><font color="#ffffff">----</font>
1.建立INI文件
<pre>
procedure TForm1.FormCreate(Sender:Tobject);
{如INI文件不存在,建立INI文件;
否则,从INI文件中读取数据}
var
status:integer;
inifile:TIniFile;
filename:String;
begin
filename:=extractfilepath
(application.exename)+’iniexample.ini’;
inifiel:=Tinifile.create(filename);
status:=inifile.readinteger
(‘MainForm’,’Status’,0);
if status< >0 then
//INI文件存在,读状态信息
case status of
1: begin
top:=inifiel.readinteger
(‘MainForm’,’Top’,top);
…………
end;
2: begin
top:=100;
left:=100;
end;
3: windowstate:=wsmaximized;
end;
end;
2.保存状态
procedure TForm1.FormDestroy
(Sender:Tobject);
var
status:integer;
begin
case windowstate of
wsnormal: begin
inifile.writeinteger
(‘MainForm’, ‘Top’,top);
…………
//保存状态信息
end;
wsminimized: status:=2;
wsmaximized: status:=3;
end;
inifile.writeinteger(‘MainForm’,
‘Status’,status);
inifile.destroy;
end;
上述程序运行之后,生成的iniexample.ini文件格式如下:
[MainForm]
Top=60
Left=100
………
Status=1
</pre>
<p><font color="#ffffff">----</font>
<b>二、 使用注册表保存状态信息</b>
<p><font color="#ffffff">----</font>
INI文件只是为了兼容以前的应用程序而存在,Windows 95/NT使用系统注册表来代替INI文件,所有For Windows 95/NT的应用软件都应该使用注册表来保存状态,这也是MicroSoft推荐的作法。Delphi提供了两个类来处理注册表。Tregistry类封装了系统注册表。而TRegIniFile类不但封装了注册表,还提供了一条简单的途径使现有的Windows 3.1应用软件升级到Windows 95/NT。TRegIniFile类提供了TIniFile类的接口,但没有使用文件,而是将数据保存在注册表中。文件名作为一个主键,默认建立在HKEY_CURRENT_USER下。相应的,INI文件中的段名作为一个键建立,段中的各项数据就变为这个键下边的各项数值。
仍以上面的例子为例,如果使用TRegIniFile类,只需改动三处:
<p><font color="#ffffff">----</font>
1.在uses列表中用registry单元代替IniFiles单元
<p><font color="#ffffff">----</font>
2.用TRegIniFile类代替TIniFile类
<p><font color="#ffffff">----</font>
3.将inifiel:=Tinifile.create(filename);改为Inifile:=TRegIniFile.Create(Filename);
<p align="right"><small><em>中国计算机世界出版服务公司版权所有</em></small>
<br>
</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -