给你两个函数。
//保存字体
function SaveFont(MidFont:TFont;FileName,CtrName:String):Boolean;
var
FontFile:TIniFile;
begin
Result:=True;
FontFile:=TIniFile.Create(FileName);
FontFile.WriteString(CtrName, 'FontName', MidFont.Name);
FontFile.WriteString(CtrName, 'FontStyle',SetFontStyle(MidFont.Style));
FontFile.WriteInteger(CtrName, 'FontCharSet', MidFont.CharSet);
FontFile.WriteInteger(CtrName, 'FontColor', MidFont.Color);
FontFile.WriteInteger(CtrName, 'FontSize', MidFont.Size );
FontFile.WriteInteger(CtrName, 'FontHeight',MidFont.Height);
FontFile.Free;
end;
//加载字体
function LoadFont(FileName,CtrName:String;var MidFont:TFont):Boolean;
var
FontFile:TIniFile;
begin
Result:=True;
FontFile:=TIniFile.Create(FileName);
MidFont.Name:=FontFile.ReadString(CtrName,'FontName','宋体');
MidFont.Style:=GetFontStyle(FontFile.ReadString(CtrName,'FontStyle','[]'));
MidFont.CharSet:=FontFile.ReadInteger(CtrName,'FontCharSet',134);
MidFont.Color:=FontFile.ReadInteger(CtrName,'FontColor',-2147483640);
MidFont.Size:=FontFile.ReadInteger(CtrName,'FontSize',9);
MidFont.Height:=FontFile.ReadInteger(CtrName,'FontHeight',-12);
FontFile.Free;
end;
function GetFontStyle(sStyle: string): TFontStyles;
//»ñµÃ×ÖÌåÑùʽ
//sStyle : ×ÖÌå×Ö·û´®ÐÎʽ
//·µ»Ø : ×ÖÌå¸ñʽ
var
MyFS : TFontStyles;
begin
MyFS := [];
if pos('fsBold', sStyle) > 0 then MyFS := MyFS + [fsBold];
if Pos('fsItalic', sStyle) > 0 then MyFS := MyFS + [fsItalic];
if Pos('fsUnderline', sStyle) > 0 then
MyFS := MyFS + [fsUnderline];
if Pos('fsStrikeOut', sStyle) > 0 then
MyFS := MyFS + [fsStrikeOut];
Result := MyFS;
end;
function SetFontStyle(FS: TFontStyles): string;
//»ñµÃ×ÖÌåÑùʽµÄ×Ö·û´®ÐÎʽ
//FS : ×ÖÌå¸ñʽ
//·µ»Ø : ×ÖÌå×Ö·û´®ÐÎʽ
var
Mystyle : string;
begin
Mystyle := '[';
if fsBold in FS then MyStyle := MyStyle + 'fsBold';
if fsItalic in FS then
if MyStyle = '[' then
MyStyle := MyStyle + 'fsItalic'
else
MyStyle := MyStyle + ',fsItalic';
if fsUnderline in FS then
if MyStyle = '[' then
MyStyle := MyStyle + 'fsUnderline'
else
MyStyle := MyStyle + ',fsUnderline';
if fsStrikeOut in FS then
if MyStyle = '[' then
MyStyle := MyStyle + 'fsStrikeOut'
else
MyStyle := MyStyle + ',fsStrikeOut';
MyStyle := MyStyle + ']';
Result := MyStyle;
end;
const
fontstylevalue: array [tfontstyle] of dword = ($0001, $0002, $0004, $0008);
function fontstyletodword(fontstyles: tfontstyles): dword;
var
fontstyle: tfontstyle;
begin
result := 0;
for fontstyle := low(tfontstyle) to high(tfontstyle) do
begin
if fontstyle in fontstyles then
inc(result, fontstylevalue[fontstyle]);
end;
end;
function dwordtofontstyle(value: dword): tfontstyles;
var
fontstyle: tfontstyle;
begin
result := [];
for fontstyle := low(tfontstyle) to high(tfontstyle) do
begin
if (fontstylevalue[fontstyle] and value) = fontstylevalue[fontstyle] then
result := result + [fontstyle];
end;
end;