当前位置:编程学习 > Delphi >>

如何在Delphi操作中将Font.Style写入INI文件

前阵子笔者编写了一个小程序在INI文件中记录字体的属性(颜色值/color,大小/size,字体名/name,样式/style),size值和color值能用数值方式直接写入INI文件,name是用字符方式写入,由于Font.style不是字符型、数值型,也不是布尔型,属于TfontStyles类,不能直接写入INI文件中去,参考了很多相关书籍也不解决这个问题,也在网络上的Delphi站点询问过,并没有满意的答复,没办法,这个问题必须自己想办法解决了,通过笔者一系列的摸索实验,最后终于找到了比较满意的解决方法,程序代码如下:

一、先在uses中加入 inifiles;

二、定义变量

var
Mystyle : string;
Myini : inifile;

三、写

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;

四、读:

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 运行就通过了。

 

www.zzzyk.com,学习电脑知识的好地方

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,