GDI+有个FontCollection类,一般情况下很少用到,很多人甚至连这些类是干什么的都不知道。FontCollection本身是个基类,它有两个派生类InstalledFontCollection和PrivateFontCollection,这两个类用好了,可以起到意想不到的作用。
InstalledFontCollection用来枚举当前系统已经安装的字体。有人经常问,为什么有些字体系统中已经存在,但使用FontFamily或Font类建立对象时往往失败,如MS Sans Serif、MS Serif等字体。这是因为GDI+只能使用矢量字体,使用InstalledFontCollection枚举一下,便知道哪些字体被GDI+支持。
PrivateFontCollection是用来建立你自己专用的字体集,我觉得这个类很方便,也很实用。有时候,程序中需要使用某些特殊字体,但往往考虑用户系统有可能没安装这些字体,便改变了方案,或者在用户使用说明书中要求用户安装某种字体,否则将达不到某种效果,甚至程序不能正常运行等。那么,这时候使用PrivateFontCollection,是你最好的选择方案之一。程序发布时,将字体文件打包进去,在需要用到这些字体时,程序自动安装字体到你的专用字体集(不会影响操作系统),供你使用。
下面的Delphi例子程序演示了InstalledFontCollection和PrivateFontCollection的使用,再次提醒,例子中使用的Gdiplus单元是本人自己改写的,与网上流通的不完全兼容,需要稍作改动才行(不能使用Wndows字体系统目录做测试,在对话框点击该目录字体,只是重新安装,不能打开。可以将字符文件拷贝到其它目录)。
unitFCMain;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
Dialogs,StdCtrls,ExtCtrls,Gdiplus;
type
TMainForm=class(TForm)
Label1:TLabel;
Label2:TLabel;
Label3:TLabel;
PaintBox1:TPaintBox;
ListBox1:TListBox;
ListBox2:TListBox;
Button1:TButton;
Button2:TButton;
OpenDialog1:TOpenDialog;
procedureFormCreate(Sender:TObject);
procedureFormDestroy(Sender:TObject);
procedureListBox1Click(Sender:TObject);
procedureButton1Click(Sender:TObject);
procedureButton2Click(Sender:TObject);
procedurePaintBox1Paint(Sender:TObject);
private
...{Privatedeclarations}
SFontCollect,FontCollect:TGpFontCollection;
PFontCollect:TGpPrivateFontCollection;
FontFamily:TGpFontFamily;
public
...{Publicdeclarations}
end;
var
MainForm:TMainForm;
implementation
usesGdipTypes;
...{$R*.dfm}
//枚举字体集FontCollect的所有字体名到List中
functionEnumFontFamily(List:TStrings;FontCollect:TGpFontCollection):Integer;
var
Familys:arrayofTGpFontFamily;
i:Integer;
begin
Result:=FontCollect.GetFamilyCount;
ifResult=0thenExit;
SetLength(Familys,Result);
List.Clear;
fori:=0toResult-1do
Familys[i]:=TGpFontFamily.Create;
FontCollect.GetFamilies(Familys);
fori:=0toResult-1do
begin
List.Add(Familys[i].GetFamilyName);
Familys[i].Free;
end;
end;
//通过打开文件对话框装入字体文件到专用字体集PFontCollect
procedureTMainForm.Button1Click(Sender:TObject);
var
i:Integer;
begin
ifnotOpenDialog1.ExecutethenExit;
try
fori:=0toOpenDialog1.Files.Count-1do
PFontCollect.AddFontFile(OpenDialog1.Files[i]);
EnumFontFamily(ListBox2.Items,PFontCollect);
except
onE:EGdiplusExceptiondoShowMessage(e.GdipErrorString);
end;
end;
procedureTMainForm.Button2Click(Sender:TObject);
begin
Close;
end;
procedureTMainForm.FormCreate(Sender:TObject);
begin
SFontCollect:=TGpInstalledFontCollection.Create;
ifEnumFontFamily(ListBox1.Items,SFontCollect)>0then
begin
ListBox1.ItemIndex:=0;
ListBox1Click(ListBox1);
end;
PFontCollect:=TGpPrivateFontCollection.Create;
end;
procedureTMainForm.FormDestroy(Sender:TObject);
begin
ifAssigned(FontFamily)then
FontFamily.Free;
PFontCollect.Free;
SFontCollect.Free;
end;
//选择系统或者专用字体集的字体名称,建立一个FontFamily供PaintBox1使用
procedureTMainForm.ListBox1Click(Sender:TObject);
begin
ifSender=ListBox1thenFontCollect:=SFontCollect
elseFontCollect:=PFontCollect;
ifAssigned(FontFamily)thenFontFamily.Free;
withSenderasTListBoxdo
FontFamily:=TGpFontFamily.Create(Items[ItemIndex],FontCollect);
PaintBox1.Invalidate;
end;
//在PaintBox1显示字体来源及对各种风格的支持
procedureTMainForm.PaintBox1Paint(Sender:TObject);
const
StyleStr:array[0..4]ofstring=
('Regular','Bold','Italic','Underline','StrikeOut');
var
I:Integer;
style:TFontStyles;
g:TGpGraphics;
font:TGpFont;
FontName,s:string;
begin
ifnotAssigned(FontFamily)thenExit;
style:=[];
g:=TGpGraphics.Create(PaintBox1.Canvas.Handle);
try
forI:=0to4do
begin
ifnotFontFamily.IsStyleAvailable(style)then
Continue;
FontName:=FontFamily.GetFamilyName;
font:=TGpFont.Create(FontName,18,style,utPixel,FontCollect);
try
ifI=0then
begin
ifFontCollect=SFontCollectthen
s:=FontName+''+'系统字体集'
else
s:=FontName+''+'专用字体集';
g.DrawString(s,font,Brushs.Red,0,10);
end;
g.DrawString(FontName+''+StyleStr[I],font,Brushs.Blue,0,25*I+40);
style:=[TFontStyle(I)];
finally
font.Free;
end;
end;
finally
g.Free;
end;
end;
end.
下面的运行结果表明安装了2种字体到专用字体集,并选择显示了1种字体效果:
如有错误请指正:maozefa@hotmail.com
分享到:
相关推荐
GDI+入门指导书------经典 非常适合于GDI+初学者
——在GDI+ Painter应用程序中添加颜色、钢笔和画笔 总结 第5章 颜色、字体和文本 5.1 访问Graphics对象 5.2 使用颜色 5.3 使用字体 5.4 使用文本和字符串 5.5 渲染文本的质量和性能 5.6 高级版式 5.7 一个...
windwos C++ gdi++实现jpg图像压缩-图像裁剪和缩放-图像格式转换-图像dpi修改
WINDOWS GDI和GDI+编程实例剖析. - READ
DELPHI版的GDI++库,内有Demos.
使用GDI+进行图形缩放、拖动,多种图片格式支持,仅简单示例。 问题源贴:http://bbs.csdn.net/topics/390638094
——在GDI+ Painter应用程序中添加颜色、钢笔和画笔 总结 第5章 颜色、字体和文本 5.1 访问Graphics对象 5.2 使用颜色 5.3 使用字体 5.4 使用文本和字符串 5.5 渲染文本的质量和性能 5.6 高级版式 5.7 一个...
GDI+程序设计 GDI+程序设计 GDI+程序设计 GDI+程序设计
GDI+程序设计.pdf,书籍和随书源码。
其中,ULONG_PTR是一个DWORD数据类型,该成员变量用来保存GDI+被初始化后在应用程序中的GDI+标识,以便能在应用程序退出后,引用该标识来调用Gdiplus:: GdiplusShutdown来关闭GDI+。 (2)在应用类中添加...
一本为C#开发人员准备的图形图像处理技术的书籍
在Delphi中使用GDI+,范例中包含GDI+各种效果的测试。
使用C#编写的贪吃蛇游戏,游戏相关说明可以参考我的博文在《WinForm中使用GDI+编写游戏--贪吃蛇》
C#GDI+小游戏 飞机大战C#GDI+小游戏 飞机大战C#GDI+小游戏 飞机大战C#GDI+小游戏 飞机大战C#GDI+小游戏 飞机大战
GDI+对话框的显示,里面有5个特效和打开文件的代码
一本不可多得的GDI+绘图书籍,网上GDI+介绍其实挺少的,研究GDI+绘图时,浪费了不少时间,找了很久,发现了一本好书,在万分激动的心情下,将此书分享给大家,希望在GDI+绘图上给您一些指导。
使用Delphi+GDI实现图片的镜像翻转,有需要的可以试试。