快捷搜索:  as  /etc/passwd  test  MTU2MDM2MzIzNQ`  as aNd 8=8

GDI+绘制自定义行距的文本(续)

在上文“GDI+绘制自定义行距的文本的三种措施。”中,先容了绘制自定义行间距的多行文本的措施。

在第三种的措施中,启用了GdipDrawDriverString这个函数。这个函数可以定义每个字符的位置,这是它的优点。不过它的毛病也对照显着。一是它定义的字符位置因此字符的左下角为基准的,和一样平常的观点是两样的。二是他对Font要求对照高,听说假如采纳的是英翰墨体,在显示中文时会显示成一个个小方块。

再翻翻GDIP中的其他函数,发明graphics的DrawString的措施都是调用GdipDrawString这个函数。假如我们能跳过graphics直接调用GdipDrawString这个函数,是不是能前进效率呢?我们碰命运运限。

gdiplus.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)> _

Friend Shared Function GdipDrawString(ByVal graphics As IntPtr, _

ByVal textString As String, _

ByVal length As Integer, _

ByVal font As IntPtr, _

ByRef layoutRect As GPRECTF, _

ByVal stringFormat As IntPtr, _

ByVal brush As IntPtr) As Integer

End Function

_

Friend Structure GPRECTF

Friend X As Single

Friend Y As Single

Friend Width As Single

Friend Height As Single

Friend Sub New(ByVal x As Single, ByVal y As Single, ByVal width As Single, ByVal height As Single)

Me.X = x

Me.Y = y

Me.Width = width

Me.Height = height

End Sub

Friend Sub New(ByVal rect As RectangleF)

Me.X = rect.X

Me.Y = rect.Y

Me.Width = rect.Width

Me.Height = rect.Height

End Sub

Friend ReadOnly Property SizeF() As SizeF

Get

Return New SizeF(Me.Width, Me.Height)

End Get

End Property

Friend Function ToRectangleF() As RectangleF

Return New RectangleF(Me.X, Me.Y, Me.Width, Me.Height)

End Function

End Structure

Public Sub Draw4(ByVal Text As String)

Clear()

Dim i As Integer, j As Integer, tS() As String

j = Int(Text.Length / 52)

ReDim tS(j - 1)

For i = 0 To j - 1

tS(i) = Text.Substring(i * 52, 52)

Next

If Text.Length - j * 52 Then

ReDim Preserve tS(j+1)

tS(j+1) = Text.Substring(j * 52)

End If

DrawLineText(mG, tS, mFont, New SolidBrush(mForeColor))

End Sub

Private Sub DrawLineText(ByVal G As Graphics, ByVal T() As String, ByVal F As Font, ByVal B As Brush)

If (G Is Nothing) Then Throw New ArgumentNullException("graphics")

If (T Is Nothing) Then Throw New ArgumentNullException("text")

If (F Is Nothing) Then Throw New ArgumentNullException("font")

If (B Is Nothing) Then Throw New ArgumentNullException("brush")

Dim Field As FieldInfo

Field = GetType(Graphics).GetField("nativeGraphics", BindingFlags.Instance Or BindingFlags.NonPublic)

Dim hGraphics As IntPtr = Field.GetValue(G)

Field = GetType(Font).GetField("nativeFont", BindingFlags.Instance Or BindingFlags.NonPublic)

Dim hFont As IntPtr = Field.GetValue(F)

Field = GetType(Brush).GetField("nativeBrush", BindingFlags.Instance Or BindingFlags.NonPublic)

Dim hBrush As IntPtr = Field.GetValue(B)

Dim i As Integer, tR As GPRECTF

tR = New GPRECTF(0, 0, mBmp.Width, mBmp.Height)

For i = 0 To 17

tR.X = 3

tR.Y = 3 + i * mLineHeight

GdipDrawString(hGraphics, T(i), T(i).Length, hFont, tR, IntPtr.Zero, hBrush)

Next

End Sub

经测试,措施四和措施三的效率平起平坐。然则,措施四更靠近我们直不雅的设法主见,而且对Font的要求也不是很高。

可能还有更好的措施,迎接各位网友交流。

转自:http://www.cnblogs.com/grenet/archive/2010/04/22/1717775.html

您可能还会对下面的文章感兴趣: