在Delphi中实现将Font.Style写入INI文件

本文介绍了一种在Delphi程序中如何将TFont.Style对象的样式信息转换为字符串并写入INI文件,以及从INI文件读取并还原字体样式的解决方案。通过检查Font.Style的不同属性,如fsBold、fsItalic等,将其转换为字符串并存储,然后在读取时再转换回TFontStyles集合。

前不久我编写一个小程序在INI文件中记录字体的属性(颜色值color,大小size,字体名name,样
式style),其中color值和size值可以用数值方式写入INI文件,name是用字符方式写入,但
Font.style不是数值型、字符型,也不是布尔型,而是TfontStyles类,无法直接写入INI文件中去,
我找了好多相关书籍也没找到方法,也到网络上的Delphi站点去问,也没得到满意的答复,没法子,
看来还得自已想办法解决,我通过一系列的摸索实验,终于找到了比较满意的解决方法,程序代码如
下:

1、先在uses中加入 inifiles;

2、定义变量

var

Mystyle : string;

Myini : inifile;

3、写

begin

Mystyle := '[';

Myini := TInifile.Create ('inifile.ini');

with FontDialog.Font do

begin

if fsBold in Style then MyStyle := MyStyle + 'fsBold';

if fsItalic in Style then

if MyStyle = '[' then

MyStyle := MyStyle + 'fsItalic'

else

MyStyle := MyStyle + ',fsItalic';

if fsUnderline in Style then

if MyStyle = '[' then

MyStyle := MyStyle + 'fsUnderline'

else

MyStyle := MyStyle + ',fsUnderline';

if fsStrikeOut in Style then

if MyStyle = '[' then

MyStyle := MyStyle + 'fsStrikeOut'

else

MyStyle := MyStyle + ',fsStrikeOut';

MyStyle := MyStyle + ']';

end;

Myini.WriteString ('FontStyle', 'style', MyStyle);

Myini.free;

End;

4、读:

var

MyFontStyle : TFontStyles;

MyStyle : string;

begin

MyFontStyle := [];

Myini := TInifile.Create ('inifile.ini');

Mystyle := Myini.ReadString ('Fontstyle', 'style', '[]');

if pos ('fsBold', Mystyle) > 0 then MyFontStyle := MyFontStyle + [fsBold];

if Pos ('fsItalic', MyStyle) > 0 then MyFontStyle := MyFontStyle + [fsItalic];

if Pos ('fsUnderline', MyStyle) > 0 then

MyFontStyle := MyFontStyle + [fsUnderline];

if Pos ('fsStrikeOut', MyStyle) > 0 then

MyFontStyle := MyFontStyle + [fsStrikeOut];

FontDialog.Font.Style := MyFontStyle;

MyIni.free;

end;

 

以上代码在Delphi 4.0 运行通过。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值