VB6 GDI+ 入门教程[4] 文字绘制

http://vistaswx.com/blog/article/category/tutorial/page/2
2009 年 6 月 18 日 7条评论
标签: ,, 分类: Tutorial,VB6 GDI+

1.GDI+中文字的必须要素

首先,与其它软件一样,GDI+中的文字也有格式。画文字有多种画法,但是无论如何,我们都需要创建一个FontFamily,这其中包含了字体类型的信息,包括字体名称、字体对齐方式(需要设置)等等。一般的画法然后还要从这个FontFamily创建一个Font,这个Font中包括字体样式(粗体、斜体)、字号等等,再后来我们调用一个函数把文字用这个Font显示出来~;路径画法(可以显示边框画法)则不需要创建字体,直接调用函数,字体的样式包括在函数里面了。

可见,GDI+中文字是需要一个FontFamily(一般是全局的),和一些Font(各种不同样式)以及文字组成的。

2.GDI+绘制文字

GDI+绘制文字有几种,下面将分别示例。

(1)标准画法:GdipDrawString

这是一般的画文字的办法,这种画法支持ClearTypeGridFit(还需要用语句再设置下),需要创建Font。

以下是主要绘图部分(窗体):

 
Option Explicit
 
Dim graphics As Long, Brush As Long
Dim fontfam As Long, strformat As Long, curfont As Long, rclayout As RECTF
 
Private Sub Form_Load()
    InitGDIPlus
 
    GdipCreateFromHDC Me.hDC, graphics
    GdipCreateFontFamilyFromName StrPtr("黑体"), 0, fontfam
    GdipCreateStringFormat 0, 0, strformat
    GdipCreateSolidFill &HFFFF0000, Brush
    GdipSetStringFormatAlign strformat, StringAlignmentNear
    GdipCreateFont fontfam, 15, FontStyle.FontStyleItalic, UnitPixel, curfont
 
    GdipSetTextRenderingHint graphics, TextRenderingHintClearTypeGridFit
 
    rclayout.Left = 100
    rclayout.Top = 100
    rclayout.Right = 150
    rclayout.Bottom = 150
 
    GdipDrawString graphics, StrPtr("Hellow world! 这是我们第一个GDI+文字~!!"), -1, curfont, rclayout, strformat, Brush
End Sub
 
Private Sub Form_Unload(Cancel As Integer)
    GdipDeleteFontFamily fontfam
    GdipDeleteStringFormat strformat
    GdipDeleteFont curfont
    GdipDeleteBrush Brush
    GdipDeleteGraphics graphics '释放graphics占用的内存
 
    TerminateGDIPlus
End Sub

Photobucket

可以看到这种画法思路是:

1.创建FontFamily (StrPtr:获取字符串指针,这样就能支持中文了!这就是不用TLB的原因……)

2.创建stringFormat(一般也可以不创),设置样式

3.创建Font。其中一定要注意单位问题。否则不要问我进去14输出的怎么不是14px大小文字……这里我们字体样式也巧妙了下,虽然声明中可以改写为As FontStyle但是不推荐。于是我们写就写FontStyle.xxx这样又可读性高,又不会出错。

4.创建Brush(显示文字咯)

5.设置文字区域(RcLayout)

6.绘制图形

7.扫地工作

这样 完美地画出了字。

注意:rectf中虽然是right,bottom但是实际上是width height,不要被误导哟。!

 

(2)路径画法:GdipAddPathString

这种画法一般用于绘制旋转文字、描边的文字等等。虽然可以设置graphics的圆滑设置,但是它画出来的文字依然不怎么清晰(相对于第一种来说)

窗体中:

 
Option Explicit
 
Dim graphics As Long, Brush As Long, Pen As Long
Dim fontFam As Long, strFormat As Long, strPath As Long, rclayout As RECTL
 
Private Sub Form_Load()
    InitGDIPlus
 
    GdipCreateFromHDC Me.hDC, graphics
    GdipSetSmoothingMode graphics, SmoothingModeAntiAlias
 
    GdipCreateFontFamilyFromName StrPtr("Verdana"), 0, fontFam
    GdipCreateStringFormat 0, 0, strFormat
    GdipSetStringFormatAlign strFormat, StringAlignmentNear
 
    GdipCreateSolidFill &HFFDEDEDE, Brush
    GdipCreatePen1 &HFF222222, 2, UnitPixel, Pen
 
    rclayout.Left = 10
    rclayout.Top = 10
    rclayout.Right = 200
    rclayout.Bottom = 150
 
    GdipCreatePath FillModeAlternate, strPath
    GdipAddPathStringI strPath, StrPtr("描边 0123"), -1, fontFam, FontStyle.FontStyleBold, 55, rclayout, strFormat
    GdipFillPath graphics, Brush, strPath
    GdipDrawPath graphics, Pen, strPath
End Sub
 
Private Sub Form_Unload(Cancel As Integer)
    GdipDeleteFontFamily fontFam
    GdipDeleteStringFormat strFormat
    GdipDeletePath strPath
    GdipDeleteBrush Brush
    GdipDeletePen Pen
 
    GdipDeleteGraphics graphics '释放graphics占用的内存
 
    TerminateGDIPlus
End Sub

Photobucket

好 回来了 我们来比较一下这个画法有什么好处。

看出来了 它可以描边……恩 我不是在上面说了嘛 它还支持旋转、合并等等。

对了 我还说过“画出来不怎么清晰”,这里好像很好嘛!其实不然。如果你把描边去掉,单单FillPath,并且把字号减小 比如14,字体样式为普通,你就会发现不清晰了~!

它的过程是这样的:

1.首先前面部分和画普通文字一样 都需要创建FontFamily还有可选的创建字体对齐格式等等。

2.接下来路径画法不需要创建Font,我们需要创建(初始化)一个路径,否则可是什么都没有哦~

3.然后我们需要把文字增加到Path中去。

4.我们要FillPath填充这个路径 或者是DrawPath描出这个路径。如果是实心文字自然就是FillPath咯

5.最后别忘了释放Pen(如果有)和Brush(如果有) 以及最后一个Path。

(3)底层画法:GdipDrawDriverString

如名,底层画法。这种画法是最底层的绘制文字,底层到了……它不会自动转换字体(比如用Verdana绘制中文字体就不会显示出来) 由于不常使用,这里不贴画法了。

原文地址:https://www.cnblogs.com/liuzhaoyzz/p/4035055.html