zoukankan      html  css  js  c++  java
  • GdiPlus[24]: IGPPrivateFontCollection: 分别从文件和内存加载字体


    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
    匿名函数
    内置函数一(待跟新)
    python 学习迭代器的认识
    python 学习笔记 —— 函数的认识
    搭建yum 源
    python 学习第六天 文件的处理方式
    python 学习第五天 字典得 增删改查
  • 原文地址:https://www.cnblogs.com/del/p/1624924.html
Copyright © 2011-2022 走看看