IGPFontFamily 的基本使用:

uses GdiPlus, GdiPlusHelpers;

procedure TForm1.FormPaint(Sender: TObject);
const
  Pt: TGPPointF = (X:10; Y:10);
var
  FontFamily: IGPFontFamily;
  Font: IGPFont;
  Brush: IGPSolidBrush;
begin
  FontFamily := TGPFontFamily.Create('微软雅黑');
  Font := TGPFont.Create(FontFamily, 16);
  Brush := TGPSolidBrush.Create($FFFF0000);
  Canvas.ToGPGraphics.DrawString(Font.Family.FamilyName, Font, Pt, Brush);
end;


IGPFontFamily 有四个获取相关尺寸的方法:
GetEmHeight、GetCellAscent、GetCellDescent、GetLineSpacing.

MSDN 上有一张相关图片:



关于以上四个方法的测试:



代码文件:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    ComboBox1: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses GdiPlus;

procedure TForm1.ComboBox1Change(Sender: TObject);
const
  fn: array[0..4] of string = ('Regular', 'Bold', 'Italic', 'Underline', 'BoldItalic');
  fs: array[0..4] of TGPFontStyle = (FontStyleRegular, [FontStyleBold],
    [FontStyleItalic], [FontStyleUnderline], FontStyleBoldItalic);
var
  FontFamily: IGPFontFamily;
  EmHeight,CellAscent,CellDescent,LineSpacing: Word;
  i: Integer;
begin
  FontFamily := TGPFontFamily.Create(ComboBox1.Text);
  Memo1.Clear;
  Memo1.Lines.BeginUpdate;
  for i := 0 to 4 do
  begin
    EmHeight := Fontfamily.GetEmHeight(fs[i]);
    CellAscent := FontFamily.GetCellAscent(fs[i]);
    CellDescent := FontFamily.GetCellAscent(fs[i]);
    LineSpacing := FontFamily.GetCellAscent(fs[i]);
    with Memo1.Lines do begin
      Add(Format('%s - %s:', [FontFamily.FamilyName, fn[i]]));
      Add(Format('EmHeight:'#9'%d', [EmHeight]));
      Add(Format('CellAscent:'#9'%d', [CellAscent]));
      Add(Format('CellDescent:'#9'%d', [CellDescent]));
      Add(Format('LineSpacing:'#9'%d', [LineSpacing]));
      Add('');
    end;
  end;
  Memo1.Lines.EndUpdate;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  FontCollection: IGPFontCollection;
  FontFamily: IGPFontFamily;
begin
  FontCollection := TGPInstalledFontCollection.Create;
  for FontFamily in FontCollection.Families do
  begin
    ComboBox1.Items.Add(FontFamily.FamilyName);
  end;
  ComboBox1.ItemIndex := ComboBox1.Items.IndexOf('宋体');
  ComboBox1.OnChange(Sender);
end;

end.


窗体文件:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 417
  ClientWidth = 388
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 0
    Top = 0
    Width = 225
    Height = 417
    Align = alLeft
    Lines.Strings = (
      'Memo1')
    ScrollBars = ssVertical
    TabOrder = 0
  end
  object ComboBox1: TComboBox
    Left = 231
    Top = 16
    Width = 145
    Height = 21
    TabOrder = 1
    Text = 'ComboBox1'
    OnChange = ComboBox1Change
  end
end

posted on 2009-12-15 16:17  万一  阅读(2848)  评论(1编辑  收藏  举报