首页 / 软件开发 / Delphi / 在Delphi中实现将Font.Style写入INI文件
在Delphi中实现将Font.Style写入INI文件2008-02-02前不久我编写一个小程序在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) $#@62; 0 then MyFontStyle := MyFontStyle + [fsBold];
if Pos (fsItalic, MyStyle) $#@62; 0 then MyFontStyle := MyFontStyle + [fsItalic];
if Pos (fsUnderline, MyStyle) $#@62; 0 then
MyFontStyle := MyFontStyle + [fsUnderline];
if (fsStrikeOut, MyStyle) $#@62; 0 then
MyFontStyle := MyFontStyle + [fsStrikeOut];
FontDialog.Font.Style := MyFontStyle;
MyIni.free;
end;
以上代码在Delphi 4.0 运行通过。