前不久我编写一个小程序在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 运行通过。
在Delphi中实现将Font.Style写入INI文件
最新推荐文章于 2024-03-25 16:46:52 发布
本文介绍了一种在Delphi程序中如何将TFont.Style对象的样式信息转换为字符串并写入INI文件,以及从INI文件读取并还原字体样式的解决方案。通过检查Font.Style的不同属性,如fsBold、fsItalic等,将其转换为字符串并存储,然后在读取时再转换回TFontStyles集合。
2827

被折叠的 条评论
为什么被折叠?



