VB.NET开发工具为我们带来了非常大的惊喜。各种新的功能特点大大吸引了开发人员的眼球。下面我们就可以通过一段对于VB.NET文字宽度计算的代码实现方式来对此进行深入的解读,了解其应用技巧。#t#
VB.NET文字宽度计算的代码示例:
- '被表示的文字列
- Dim s As String =
"我是全哥,欢迎!" - 'PictureBox1的Graphics对象取得
- Dim g As Graphics =
PictureBox1.CreateGraphics() - '字体对象作成
- Dim fnt As New Font("宋体", 20)
- 'StringFormat对象作成
- Dim sf As New StringFormat
- '描绘文字列
- g.DrawString(s, fnt, Brushes.Black,
0, 0, sf) - '设定宽度为1000像素,文字列描绘时计算大小
- Dim stringSize As SizeF =
g.MeasureString(s, fnt, 1000, sf) - '取得的文字列的大小使用矩形描绘
- g.DrawRectangle(Pens.Red, 0, 0,
stringSize.Width, stringSize.Height)
- '测量"BINGNING"和"NET"的大小
- '测量指定文字列的范围
- Dim characterRanges As
CharacterRange() = _- {New CharacterRange(0, 4),
New CharacterRange(5, 3)}- sf.SetMeasurableCharacterRanges
(characterRanges)- '指定描绘文字列的矩形
- Dim layoutRect As New
RectangleF(0, 0, 500, 100)- '取得Region数组
- Dim stringRegions As Region() = _
- g.MeasureCharacterRanges(s,
fnt, layoutRect, sf)- '描绘取得文字列大小的矩形
- Dim rect1 As RectangleF =
stringRegions(0).GetBounds(g)- g.DrawRectangle(Pens.Yellow,
Rectangle.Round(rect1))- Dim rect2 As RectangleF =
stringRegions(1).GetBounds(g)- g.DrawRectangle(Pens.Blue,
Rectangle.Round(rect2))- '资源释放
- fnt.Dispose()
- g.Dispose()
VB.NET文字宽度计算相关操作方法就为大家介绍到这里。