文档章节

GdiPlus[43]: IGPGraphics (二) 关于文本绘制

涂孟超
 涂孟超
发布于 2014/09/26 15:37
字数 671
阅读 6
收藏 0

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;

 
 
 
 
 

 

 

  

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

 
 
 
 
 

 

 

  

本文转载自:http://www.cnblogs.com/del/archive/2009/12/22/1629717.html

共有 人打赏支持
涂孟超
粉丝 12
博文 2011
码字总数 14107
作品 0
深圳
程序员
私信 提问
MFC边框重绘

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 pragma once incl......

江河海流
2016/01/30
509
0
第155天:canvas(二)

一、添加样式和颜色 在前面的绘制矩形章节中,只用到了默认的线条和颜色。 如果想要给图形上色,有两个重要的属性可以做到。 设置图形的填充颜色 设置图形轮廓的颜色   备注: 1 function...

半指温柔乐
01/24
0
0
ABBYY找不到图片如何处理

ABBYY FineReader 12是一款图文识别(OCR)软件,使用ABBYY FineReader 12识别带有图片的文档时,图片区域标识文档中所包含的图片。如果不想识别某一文本片段,也可使用“图片”区域,如果程序...

ABBYY
2016/09/18
14
0
GDI+里面的问题。。。。。。。。。。

error C2248: “Gdiplus::Bitmap::Bitmap”: 无法访问 private 成员(在“Gdiplus::Bitmap”类中声明) 1> c:program filesmicrosoft sdkswindowsv7.0aincludegdiplusheaders.h(630) : 参见“G......

xxdd
2013/10/24
60
0
vs 2005 添加GDI配置

在VS2005当中由于自带了GDI+的开发包,所以不需要在安装GDI开发包,这些头文件的位置由VS中的$(WindowsSdkDir)/include自动包含进来了 下午自己添加了好几次,总是出现有报错,,在 stdafx.h...

种地瓜
2015/12/08
85
0

没有更多内容

加载失败,请刷新页面

加载更多

MariaDB 服务器在 MySQL Workbench 备份数据的时候出错如何解决

服务器是运行在 MariaDB 10.2 上面的,在使用 MySQL Workbench 出现错误: mysqldump: Couldn't execute 'SELECT COLUMN_NAME, JSON_EXTRACT(HISTOGRAM, '$."number-of-buckets-specified"'......

honeymose
今天
3
0
apache顶级项目(二) - B~C

apache顶级项目(二) - B~C https://www.apache.org/ Bahir Apache Bahir provides extensions to multiple distributed analytic platforms, extending their reach with a diversity of s......

晨猫
今天
6
0
day152-2018-11-19-英语流利阅读

“超级食物”竟然是营销噱头? Daniel 2018-11-19 1.今日导读 近几年来,超级食物 superfoods 开始逐渐走红。不难发现,越来越多的轻食餐厅也在不断推出以超级食物为主打食材的健康料理,像是...

飞鱼说编程
今天
13
0
SpringBoot源码:启动过程分析(二)

接着上篇继续分析 SpringBoot 的启动过程。 SpringBoot的版本为:2.1.0 release,最新版本。 一.时序图 一样的,我们先把时序图贴上来,方便理解: 二.源码分析 回顾一下,前面我们分析到了下...

Jacktanger
昨天
4
0
Apache防盗链配置,Directory访问控制,FilesMatch进行访问控制

防盗链配置 通过限制referer来实现防盗链的功能 配置前,使用curl -e 指定referer [root@test-a test-webroot]# curl -e "http://www.test.com/1.html" -x127.0.0.1:80 "www.test.com/1.jpg......

野雪球
昨天
7
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部