IGPGraphics 中绘制文本相关的方法和属性:

IGPGraphics.DrawString();             { 绘制文本 }
IGPGraphics.MeasureString();          { 获取指定格式的字符串需要的矩形 }

IGPGraphics.MeasureCharacterRanges(); { 获取指定字符所在的区域 }

IGPGraphics.DrawDriverString();       { 绘制可指定间距的文本 }
IGPGraphics.MeasureDriverString();    { 获取可指定间距文本所需要的矩形范围 }

IGPGraphics.TextRenderingHint;        { 获取或设置文本呈现模式 }
IGPGraphics.TextContrast;             { 灰度校正值(消除锯齿和 ClearType 文本的伽玛值校正: 0..12, 默认是 4) }



DrawString 的三种用法:



uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  str: string;
  Font: IGPFont;
  Pt: TGPPointF;
  Brush: IGPSolidBrush;
  StringFormat: IGPStringFormat;
  Rect: TGPRectF;
begin
  Graphics := TGPGraphics.Create(Handle);
  //
  str := 'Delphi 2010';
  Font := TGPFont.Create('Arial Black', 16);
  Pt.Initialize(0, 10);
  Brush := TGPSolidBrush.Create($FF228B22);
  Graphics.DrawString(str, Font, Pt, Brush);
  //
  Pt.X := ClientWidth;
  Brush.Color := $FF0000FF;
  StringFormat := TGPStringFormat.Create([StringFormatFlagsDirectionRightToLeft]);
  Graphics.DrawString(str, Font, Pt, StringFormat, Brush);
  //
  Brush.Color := $FFFF0000;
  Rect.Initialize(0, 0, ClientWidth, ClientHeight);
  StringFormat.FormatFlags := [StringFormatFlagsDirectionVertical];
  StringFormat.Alignment := StringAlignmentCenter;
  StringFormat.LineAlignment := StringAlignmentCenter;
  Graphics.DrawString(str, Font, Rect, StringFormat, Brush);
end;


MeasureString 测试:



uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  str: string;
  Font: IGPFont;
  Pt: TGPPointF;
  Brush: IGPSolidBrush;
  Pen: IGPPen;
  StringFormat: IGPStringFormat;
  Rect, MeasureRect: TGPRectF;
begin
  Graphics := TGPGraphics.Create(Handle);
  Graphics.Clear($FFFFFFFF);
  Brush := TGPSolidBrush.Create($FF228B22);
  Pen := TGPPen.Create($FF000000);
  //
  str := 'Delphi 2010';
  Font := TGPFont.Create('Arial Black', 16);
  Pt.Initialize(10, 10);

  Graphics.DrawString(str, Font, Pt, Brush);
  MeasureRect := Graphics.MeasureString(str, Font, Pt, nil);
  Graphics.DrawRectangle(Pen, MeasureRect);

  //
  Brush.Color := $FFFF0000;
  Rect.Initialize(0, 0, ClientWidth, ClientHeight);
  StringFormat := TGPStringFormat.Create;
  StringFormat.Alignment := StringAlignmentCenter;
  StringFormat.LineAlignment := StringAlignmentCenter;

  Graphics.DrawString(str, Font, Rect, StringFormat, Brush);
  MeasureRect := Graphics.MeasureString(str, Font, Rect, StringFormat);
  Graphics.DrawRectangle(Pen, MeasureRect);
end;


SetMeasurableCharacterRanges 测试:



uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  str: string;
  Font: IGPFont;
  CharRange1,CharRange2: TGPCharacterRange;
  CharRanges: IGPCharacterRanges;
  Rect: TGPRectF;
  StringFormat: IGPStringFormat;
  Brush,BrushBack: IGPBrush;
  Regions: IGPRegions;
  R1,R2: TGPRectF;
begin
  Graphics := TGPGraphics.Create(Handle);
  str := '万一的 Delphi 博客';
  Font := TGPFont.Create('微软雅黑', 20);
  Brush := TGPSolidBrush.Create($FFFF0000);
  BrushBack := TGPSolidBrush.Create($FFC0C0C0);

  CharRanges := TGPArray<TGPCharacterRange>.Create(2);
  CharRange1.Initialize(0, 2);
  CharRange2.Initialize(4, 6);
  CharRanges[0] := CharRange1;
  CharRanges[1] := CharRange2;

  StringFormat := TGPStringFormat.Create;
  StringFormat.SetMeasurableCharacterRanges(Charranges);

  Rect.Initialize(20, 20, ClientWidth, ClientHeight);

  Regions := Graphics.MeasureCharacterRanges(str, Font, Rect, StringFormat);
  Regions[0].GetBounds(R1, Graphics);
  Regions[1].GetBounds(R2, Graphics);
  Graphics.FillRectangle(BrushBack, R1);
  Graphics.FillRectangle(BrushBack, R2);

  Graphics.DrawString(str, Font, Rect.Location, Brush);
end;


DrawDriverString 测试:



uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Font: IGPFont;
  Brush: IGPSolidBrush;
  Pts: array of TGPPointF;
  str: string;
  i: Integer;
begin
  Graphics := TGPGraphics.Create(Canvas.Handle);
  Brush := TGPSolidBrush.Create($FF8A2BE2);

  str := 'ABCDEFG';
  SetLength(Pts, Length(str));
  for i := 0 to Length(str) - 1 do
  begin
    Pts[i].X := i * 50 + 20;
    Pts[i].Y := 60;
  end;

  Font := TGPFont.Create('Arial Black', 32);
  Graphics.DrawDriverString(PUint16(PChar(str)),
                            Length(str),
                            Font,
                            Brush,
                            @Pts[0],
                            [DriverStringOptionsCmapLookup],
                            nil
  );
end;


IGPGraphics.TextRenderingHint 测试:



{
TextRenderingHintSystemDefault            // 使用字体的默认设置
TextRenderingHintSingleBitPerPixelGridFit // 符号位图 + hinting 技术(改善字符主干和弯曲部分)
TextRenderingHintSingleBitPerPixel        // 符号位图(不使用 hinting 技术时字体会更宽一些)
TextRenderingHintAntiAliasGridFit         // 消除锯齿 + hinting 技术
TextRenderingHintAntiAlias                // 消除锯齿
TextRenderingHintClearTypeGridFit         // ClearType 文本 + hinting 技术
}

uses GdiPlus;

procedure TForm1.FormPaint(Sender: TObject);
var
  Graphics: IGPGraphics;
  Brush: IGPSolidBrush;
  Font: IGPFont;
  Pt: TGPPointF;
begin
  Graphics := TGPGraphics.Create(Handle);
  Brush := TGPSolidBrush.Create($FFB22222);
  Font := TGPFont.Create('Arial Black', 24);

  Pt.Initialize(10, 10);
  Graphics.TextRenderingHint := TextRenderingHintSystemDefault;
  Graphics.DrawString('SystemDefault', Font, Pt, Brush);

  Pt.Y := Pt.Y + Font.GetHeight(Graphics) + 10;
  Graphics.TextRenderingHint := TextRenderingHintSingleBitPerPixelGridFit;
  Graphics.DrawString('SingleBitPerPixelGridFit', Font, Pt, Brush);

  Pt.Y := Pt.Y + Font.GetHeight(Graphics) + 10;
  Graphics.TextRenderingHint := TextRenderingHintSingleBitPerPixel;
  Graphics.DrawString('SingleBitPerPixel', Font, Pt, Brush);

  Pt.Y := Pt.Y + Font.GetHeight(Graphics) + 10;
  Graphics.TextRenderingHint := TextRenderingHintAntiAliasGridFit;
  Graphics.DrawString('AntiAliasGridFit', Font, Pt, Brush);

  Pt.Y := Pt.Y + Font.GetHeight(Graphics) + 10;
  Graphics.TextRenderingHint := TextRenderingHintAntiAlias;
  Graphics.DrawString('AntiAlias', Font, Pt, Brush);

  Pt.Y := Pt.Y + Font.GetHeight(Graphics) + 10;
  Graphics.TextRenderingHint := TextRenderingHintClearTypeGridFit;
  Graphics.DrawString('ClearTypeGridFit', Font, Pt, Brush);
end;


//后接 "关于文本绘制续"

posted on 2009-12-22 13:54  万一  阅读(8710)  评论(11编辑  收藏  举报