GDI+有个FontCollection类,一般情况下很少用到,很多人甚至连这些类是干什么的都不知道。FontCollection本身是个基类,它有两个派生类InstalledFontCollection和PrivateFontCollection,这两个类用好了,可以起到意想不到的作用。

InstalledFontCollection用来枚举当前系统已经安装的字体。有人经常问,为什么有些字体系统中已经存在,但使用FontFamily或Font类建立对象时往往失败,如MS Sans Serif、MS Serif等字体。这是因为GDI+只能使用矢量字体,使用InstalledFontCollection枚举一下,便知道哪些字体被GDI+支持。

PrivateFontCollection是用来建立你自己专用的字体集,我觉得这个类很方便,也很实用。有时候,程序中需要使用某些特殊字体,但往往考虑用户系统有可能没安装这些字体,便改变了方案,或者在用户使用说明书中要求用户安装某种字体,否则将达不到某种效果,甚至程序不能正常运行等。那么,这时候使用PrivateFontCollection,是你最好的选择方案之一。程序发布时,将字体文件打包进去,在需要用到这些字体时,程序自动安装字体到你的专用字体集(不会影响操作系统),供你使用。

下面的Delphi例子程序演示了InstalledFontCollection和PrivateFontCollection的使用,再次提醒,例子中使用的Gdiplus单元是本人自己改写的,与网上流通的不完全兼容,需要稍作改动才行(不能使用Wndows字体系统目录做测试,在对话框点击该目录字体,只是重新安装,不能打开。可以将字符文件拷贝到其它目录)。

GDI+ 在Delphi程序的应用 -- FontCollectionunitFCMain;
GDI+ 在Delphi程序的应用 -- FontCollection
GDI+ 在Delphi程序的应用 -- FontCollection
interface
GDI+ 在Delphi程序的应用 -- FontCollection
GDI+ 在Delphi程序的应用 -- FontCollectionuses
GDI+ 在Delphi程序的应用 -- FontCollectionWindows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
GDI+ 在Delphi程序的应用 -- FontCollectionDialogs,StdCtrls,ExtCtrls,Gdiplus;
GDI+ 在Delphi程序的应用 -- FontCollection
GDI+ 在Delphi程序的应用 -- FontCollectiontype
GDI+ 在Delphi程序的应用 -- FontCollectionTMainForm
=class(TForm)
GDI+ 在Delphi程序的应用 -- FontCollectionLabel1:TLabel;
GDI+ 在Delphi程序的应用 -- FontCollectionLabel2:TLabel;
GDI+ 在Delphi程序的应用 -- FontCollectionLabel3:TLabel;
GDI+ 在Delphi程序的应用 -- FontCollectionPaintBox1:TPaintBox;
GDI+ 在Delphi程序的应用 -- FontCollectionListBox1:TListBox;
GDI+ 在Delphi程序的应用 -- FontCollectionListBox2:TListBox;
GDI+ 在Delphi程序的应用 -- FontCollectionButton1:TButton;
GDI+ 在Delphi程序的应用 -- FontCollectionButton2:TButton;
GDI+ 在Delphi程序的应用 -- FontCollectionOpenDialog1:TOpenDialog;
GDI+ 在Delphi程序的应用 -- FontCollectionprocedureFormCreate(Sender:TObject);
GDI+ 在Delphi程序的应用 -- FontCollectionprocedureFormDestroy(Sender:TObject);
GDI+ 在Delphi程序的应用 -- FontCollectionprocedureListBox1Click(Sender:TObject);
GDI+ 在Delphi程序的应用 -- FontCollectionprocedureButton1Click(Sender:TObject);
GDI+ 在Delphi程序的应用 -- FontCollectionprocedureButton2Click(Sender:TObject);
GDI+ 在Delphi程序的应用 -- FontCollectionprocedurePaintBox1Paint(Sender:TObject);
GDI+ 在Delphi程序的应用 -- FontCollection
private
GDI+ 在Delphi程序的应用 -- FontCollectionGDI+ 在Delphi程序的应用 -- FontCollection
...{Privatedeclarations}
GDI+ 在Delphi程序的应用 -- FontCollectionSFontCollect,FontCollect:TGpFontCollection;
GDI+ 在Delphi程序的应用 -- FontCollectionPFontCollect:TGpPrivateFontCollection;
GDI+ 在Delphi程序的应用 -- FontCollectionFontFamily:TGpFontFamily;
GDI+ 在Delphi程序的应用 -- FontCollection
public
GDI+ 在Delphi程序的应用 -- FontCollectionGDI+ 在Delphi程序的应用 -- FontCollection
...{Publicdeclarations}
GDI+ 在Delphi程序的应用 -- FontCollectionend;
GDI+ 在Delphi程序的应用 -- FontCollection
GDI+ 在Delphi程序的应用 -- FontCollectionvar
GDI+ 在Delphi程序的应用 -- FontCollectionMainForm:TMainForm;
GDI+ 在Delphi程序的应用 -- FontCollection
GDI+ 在Delphi程序的应用 -- FontCollectionimplementation
GDI+ 在Delphi程序的应用 -- FontCollection
GDI+ 在Delphi程序的应用 -- FontCollectionusesGdipTypes;
GDI+ 在Delphi程序的应用 -- FontCollection
GDI+ 在Delphi程序的应用 -- FontCollectionGDI+ 在Delphi程序的应用 -- FontCollection
...{$R*.dfm}
GDI+ 在Delphi程序的应用 -- FontCollection
//枚举字体集FontCollect的所有字体名到List中
GDI+ 在Delphi程序的应用 -- FontCollection
functionEnumFontFamily(List:TStrings;FontCollect:TGpFontCollection):Integer;
GDI+ 在Delphi程序的应用 -- FontCollectionvar
GDI+ 在Delphi程序的应用 -- FontCollectionFamilys:arrayofTGpFontFamily;
GDI+ 在Delphi程序的应用 -- FontCollectioni:Integer;
GDI+ 在Delphi程序的应用 -- FontCollectionbegin
GDI+ 在Delphi程序的应用 -- FontCollectionResult:
=FontCollect.GetFamilyCount;
GDI+ 在Delphi程序的应用 -- FontCollection
ifResult=0thenExit;
GDI+ 在Delphi程序的应用 -- FontCollectionSetLength(Familys,Result);
GDI+ 在Delphi程序的应用 -- FontCollectionList.Clear;
GDI+ 在Delphi程序的应用 -- FontCollection
fori:=0toResult-1do
GDI+ 在Delphi程序的应用 -- FontCollectionFamilys[i]:
=TGpFontFamily.Create;
GDI+ 在Delphi程序的应用 -- FontCollectionFontCollect.GetFamilies(Familys);
GDI+ 在Delphi程序的应用 -- FontCollection
fori:=0toResult-1do
GDI+ 在Delphi程序的应用 -- FontCollectionbegin
GDI+ 在Delphi程序的应用 -- FontCollectionList.Add(Familys[i].GetFamilyName);
GDI+ 在Delphi程序的应用 -- FontCollectionFamilys[i].Free;
GDI+ 在Delphi程序的应用 -- FontCollectionend;
GDI+ 在Delphi程序的应用 -- FontCollectionend;
GDI+ 在Delphi程序的应用 -- FontCollection
//通过打开文件对话框装入字体文件到专用字体集PFontCollect
GDI+ 在Delphi程序的应用 -- FontCollection
procedureTMainForm.Button1Click(Sender:TObject);
GDI+ 在Delphi程序的应用 -- FontCollectionvar
GDI+ 在Delphi程序的应用 -- FontCollectioni:Integer;
GDI+ 在Delphi程序的应用 -- FontCollectionbegin
GDI+ 在Delphi程序的应用 -- FontCollection
ifnotOpenDialog1.ExecutethenExit;
GDI+ 在Delphi程序的应用 -- FontCollection
try
GDI+ 在Delphi程序的应用 -- FontCollection
fori:=0toOpenDialog1.Files.Count-1do
GDI+ 在Delphi程序的应用 -- FontCollectionPFontCollect.AddFontFile(OpenDialog1.Files[i]);
GDI+ 在Delphi程序的应用 -- FontCollectionEnumFontFamily(ListBox2.Items,PFontCollect);
GDI+ 在Delphi程序的应用 -- FontCollectionexcept
GDI+ 在Delphi程序的应用 -- FontCollectiononE:EGdiplusException
doShowMessage(e.GdipErrorString);
GDI+ 在Delphi程序的应用 -- FontCollectionend;
GDI+ 在Delphi程序的应用 -- FontCollectionend;
GDI+ 在Delphi程序的应用 -- FontCollection
GDI+ 在Delphi程序的应用 -- FontCollectionprocedureTMainForm.Button2Click(Sender:TObject);
GDI+ 在Delphi程序的应用 -- FontCollectionbegin
GDI+ 在Delphi程序的应用 -- FontCollectionClose;
GDI+ 在Delphi程序的应用 -- FontCollectionend;
GDI+ 在Delphi程序的应用 -- FontCollection
GDI+ 在Delphi程序的应用 -- FontCollectionprocedureTMainForm.FormCreate(Sender:TObject);
GDI+ 在Delphi程序的应用 -- FontCollectionbegin
GDI+ 在Delphi程序的应用 -- FontCollectionSFontCollect:
=TGpInstalledFontCollection.Create;
GDI+ 在Delphi程序的应用 -- FontCollection
ifEnumFontFamily(ListBox1.Items,SFontCollect)>0then
GDI+ 在Delphi程序的应用 -- FontCollectionbegin
GDI+ 在Delphi程序的应用 -- FontCollectionListBox1.ItemIndex:
=0;
GDI+ 在Delphi程序的应用 -- FontCollectionListBox1Click(ListBox1);
GDI+ 在Delphi程序的应用 -- FontCollectionend;
GDI+ 在Delphi程序的应用 -- FontCollectionPFontCollect:
=TGpPrivateFontCollection.Create;
GDI+ 在Delphi程序的应用 -- FontCollectionend;
GDI+ 在Delphi程序的应用 -- FontCollection
GDI+ 在Delphi程序的应用 -- FontCollectionprocedureTMainForm.FormDestroy(Sender:TObject);
GDI+ 在Delphi程序的应用 -- FontCollectionbegin
GDI+ 在Delphi程序的应用 -- FontCollection
ifAssigned(FontFamily)then
GDI+ 在Delphi程序的应用 -- FontCollectionFontFamily.Free;
GDI+ 在Delphi程序的应用 -- FontCollectionPFontCollect.Free;
GDI+ 在Delphi程序的应用 -- FontCollectionSFontCollect.Free;
GDI+ 在Delphi程序的应用 -- FontCollectionend;
GDI+ 在Delphi程序的应用 -- FontCollection
//选择系统或者专用字体集的字体名称,建立一个FontFamily供PaintBox1使用
GDI+ 在Delphi程序的应用 -- FontCollection
procedureTMainForm.ListBox1Click(Sender:TObject);
GDI+ 在Delphi程序的应用 -- FontCollectionbegin
GDI+ 在Delphi程序的应用 -- FontCollection
ifSender=ListBox1thenFontCollect:=SFontCollect
GDI+ 在Delphi程序的应用 -- FontCollection
elseFontCollect:=PFontCollect;
GDI+ 在Delphi程序的应用 -- FontCollection
ifAssigned(FontFamily)thenFontFamily.Free;
GDI+ 在Delphi程序的应用 -- FontCollectionwithSender
asTListBoxdo
GDI+ 在Delphi程序的应用 -- FontCollectionFontFamily:
=TGpFontFamily.Create(Items[ItemIndex],FontCollect);
GDI+ 在Delphi程序的应用 -- FontCollectionPaintBox1.Invalidate;
GDI+ 在Delphi程序的应用 -- FontCollectionend;
GDI+ 在Delphi程序的应用 -- FontCollection
//在PaintBox1显示字体来源及对各种风格的支持
GDI+ 在Delphi程序的应用 -- FontCollection
procedureTMainForm.PaintBox1Paint(Sender:TObject);
GDI+ 在Delphi程序的应用 -- FontCollection
const
GDI+ 在Delphi程序的应用 -- FontCollectionStyleStr:array[
0..4]ofstring=
GDI+ 在Delphi程序的应用 -- FontCollection(
'Regular','Bold','Italic','Underline','StrikeOut');
GDI+ 在Delphi程序的应用 -- FontCollectionvar
GDI+ 在Delphi程序的应用 -- FontCollectionI:Integer;
GDI+ 在Delphi程序的应用 -- FontCollectionstyle:TFontStyles;
GDI+ 在Delphi程序的应用 -- FontCollectiong:TGpGraphics;
GDI+ 在Delphi程序的应用 -- FontCollectionfont:TGpFont;
GDI+ 在Delphi程序的应用 -- FontCollectionFontName,s:
string;
GDI+ 在Delphi程序的应用 -- FontCollectionbegin
GDI+ 在Delphi程序的应用 -- FontCollection
ifnotAssigned(FontFamily)thenExit;
GDI+ 在Delphi程序的应用 -- FontCollectionstyle:
=[];
GDI+ 在Delphi程序的应用 -- FontCollectiong:
=TGpGraphics.Create(PaintBox1.Canvas.Handle);
GDI+ 在Delphi程序的应用 -- FontCollection
try
GDI+ 在Delphi程序的应用 -- FontCollection
forI:=0to4do
GDI+ 在Delphi程序的应用 -- FontCollectionbegin
GDI+ 在Delphi程序的应用 -- FontCollection
ifnotFontFamily.IsStyleAvailable(style)then
GDI+ 在Delphi程序的应用 -- FontCollectionContinue;
GDI+ 在Delphi程序的应用 -- FontCollectionFontName:
=FontFamily.GetFamilyName;
GDI+ 在Delphi程序的应用 -- FontCollectionfont:
=TGpFont.Create(FontName,18,style,utPixel,FontCollect);
GDI+ 在Delphi程序的应用 -- FontCollection
try
GDI+ 在Delphi程序的应用 -- FontCollection
ifI=0then
GDI+ 在Delphi程序的应用 -- FontCollectionbegin
GDI+ 在Delphi程序的应用 -- FontCollection
ifFontCollect=SFontCollectthen
GDI+ 在Delphi程序的应用 -- FontCollections:
=FontName+''+'系统字体集'
GDI+ 在Delphi程序的应用 -- FontCollection
else
GDI+ 在Delphi程序的应用 -- FontCollections:
=FontName+''+'专用字体集';
GDI+ 在Delphi程序的应用 -- FontCollectiong.DrawString(s,font,Brushs.Red,
0,10);
GDI+ 在Delphi程序的应用 -- FontCollectionend;
GDI+ 在Delphi程序的应用 -- FontCollectiong.DrawString(FontName
+''+StyleStr[I],font,Brushs.Blue,0,25*I+40);
GDI+ 在Delphi程序的应用 -- FontCollectionstyle:
=[TFontStyle(I)];
GDI+ 在Delphi程序的应用 -- FontCollection
finally
GDI+ 在Delphi程序的应用 -- FontCollectionfont.Free;
GDI+ 在Delphi程序的应用 -- FontCollectionend;
GDI+ 在Delphi程序的应用 -- FontCollectionend;
GDI+ 在Delphi程序的应用 -- FontCollection
finally
GDI+ 在Delphi程序的应用 -- FontCollectiong.Free;
GDI+ 在Delphi程序的应用 -- FontCollectionend;
GDI+ 在Delphi程序的应用 -- FontCollectionend;
GDI+ 在Delphi程序的应用 -- FontCollection
GDI+ 在Delphi程序的应用 -- FontCollectionend.

下面的运行结果表明安装了2种字体到专用字体集,并选择显示了1种字体效果:

GDI+ 在Delphi程序的应用 -- FontCollection

如有错误请指正:[email protected]

相关文章: