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;
//後接 "關於文本繪制續"