前不久我编写一个小程序在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 运行通过。
式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 运行通过。