Gid+私有字体

3 篇文章 0 订阅

http://msdn.microsoft.com/en-us/library/ms536186(v=vs.85)

http://www.cnblogs.com/del/archive/2009/12/15/1624924.html


VOID Example_GetFamilies(HDC hdc)
{
   Graphics graphics(hdc);

   // Create a PrivateFontCollection object, and add three families.
   PrivateFontCollection fontCollection;
   fontCollection.AddFontFile(L"C:\\WINNT\\Fonts\\Arial.ttf");
   fontCollection.AddFontFile(L"C:\\WINNT\\Fonts\\CourBI.ttf");
   fontCollection.AddFontFile(L"C:\\WINNT\\Fonts\\TimesBd.ttf");

   // Create an array to hold the font families, and get the font families of
   // fontCollection.
   FontFamily families[3];
   int numFamilies;
   fontCollection.GetFamilies(3, families, &numFamilies);

   // Create a Font object from the first FontFamily object in the array.
   Font myFont(&families[0], 16);

   // Use myFont to draw text.
   SolidBrush solidbrush(Color(255, 0, 0, 0));
   WCHAR string[] = L"This is an Arial font";
   graphics.DrawString(string,
                       21, &myFont, PointF(0, 0), &solidbrush);
}


IGPPrivateFontCollection.AddFontFile: 从文件加载字体;

IGPPrivateFontCollection.AddMemoryFont: 从内存加载字体



AddFontFile 测试:

uses GdiPlus;

function GetFontsDirectory: string;
var
  Len: Integer;
  buf: array[0..MAX_PATH-1] of Char;
begin
  GetWindowsDirectory(buf, Length(buf));
  Result := buf + '\Fonts\';
end;

procedure TForm1.FormPaint(Sender: TObject);
var
  FontCollection: IGPPrivateFontCollection;
  FontFamily: IGPFontFamily;
  Font: IGPFont;
  Graphics: IGPGraphics;
  Brush: IGPSolidBrush;
  Point: TGPPointF;
begin
  FontCollection := TGPPrivateFontCollection.Create;
  FontCollection.AddFontFile(GetFontsDirectory + 'msyh.ttf');   //微软雅黑
  FontCollection.AddFontFile(GetFontsDirectory + 'simhei.ttf'); //黑体
  FontCollection.AddFontFile(GetFontsDirectory + 'SIMLI.TTF');  //隶书
  FontCollection.AddFontFile(GetFontsDirectory + 'simkai.ttf'); //楷体 GB2312

  Graphics := TGPGraphics.Create(Handle);
  Brush := TGPSolidBrush.Create(TGPColor.Red);
  Point.Initialize(10, 10);
  for FontFamily in FontCollection.Families do
  begin
    Font := TGPFont.Create(FontFamily, 16, FontStyleRegular, UnitPixel);
    Graphics.DrawString(FontFamily.FamilyName, Font, Point, Brush);
    Point.Y := Point.Y + Font.GetHeight(0); //参数是 0 或 nil 时, 字体建立是必须指定 UnitPixel
  end;
end;

  
  

AddMemoryFont 测试:

uses GdiPlus;

procedure TForm1.Button1Click(Sender: TObject);
var
  Stream: TMemoryStream;
  FontCollection: IGPPrivateFontCollection;
  FontFamily: IGPFontFamily;
  Font: IGPFont;
  Graphics: IGPGraphics;
  Brush: IGPSolidBrush;
  Point: TGPPointF;
begin
  Stream := TMemoryStream.Create;
  Stream.LoadFromFile('C:\Windows\Fonts\msyhbd.ttf');

  FontCollection := TGPPrivateFontCollection.Create;
  FontCollection.AddMemoryFont(Stream.Memory, Stream.Size);
  FontFamily := FontCollection.Families[0];

  Font := TGPFont.Create(FontFamily.FamilyName, 24);
  Brush := TGPSolidBrush.Create(TGPColor.Create(0, 0, 0));
  Point.Initialize(10, 10);
  Graphics := TGPGraphics.Create(Handle);

  Graphics.DrawString(FontFamily.FamilyName, Font, Point, Brush);

  Stream.Free;
end;

  
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值