程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> GdiPlus[24]: IGPPrivateFontCollection: 分別從文件和內存加載字體

GdiPlus[24]: IGPPrivateFontCollection: 分別從文件和內存加載字體

編輯:Delphi

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; 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved