GdiPlus[24]:IGPPrivateFontCollection:それぞれファイルとメモリからフォントをロード
2253 ワード
IGPPrivateFontCollection.AddFontFile:ファイルからフォントをロードします.
IGPPrivateFontCollection.AddMemoryFont:メモリからフォントをロード
AddFontFileテスト:
AddMemoryFontテスト:
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;