Public Class KerningFont
Implements IDisposable
'//APIs
<System.Runtime.InteropServices.DllImport("gdi32")> _
Protected Shared Function SelectObject(ByVal handle As IntPtr, _
ByVal hgdiobj As IntPtr) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("gdi32")> _
Protected Shared Function SetTextCharacterExtra(ByVal handle As IntPtr, _
ByVal nCharExtra As Integer) _
As Integer
End Function
<System.Runtime.InteropServices.DllImport("gdi32")> _
Protected Shared Function TextOut(ByVal handle As IntPtr, ByVal x As Integer, _
ByVal y As Integer, ByVal text As String, _
ByVal charCount As Integer) As Boolean
End Function
<System.Runtime.InteropServices.DllImport("gdi32", EntryPoint:="SetBkColor")> _
Protected Shared Function SetBackColor(ByVal handle As IntPtr, _
ByVal color As Integer) As UInt32
End Function
<System.Runtime.InteropServices.DllImport("gdi32")> _
Protected Shared Function DeleteObject(ByVal handle As IntPtr) As Boolean
End Function
<System.Runtime.InteropServices.DllImport("gdi32")> _
Protected Shared Function SetTextColor(ByVal handle As IntPtr, _
ByVal color As Integer) As UInt32
End Function
'//fields
Private disposedValue As Boolean = False
'//properties
Private _font As Font = Nothing
Public ReadOnly Property Font() As Font
Get
Return Me._font
End Get
End Property
Private _kerning As Integer = 0
Public Property Kerning() As Integer
Get
Return Me._kerning
End Get
Set(ByVal value As Integer)
Me._kerning = value
End Set
End Property
'//constructors
Public Sub New(ByVal f As Font, _
ByVal kerning As Integer)
Me._kerning = kerning
Me._font = f
End Sub
'//methods
Public Sub DrawString(ByVal c As Control, ByVal text As String, _
ByVal x As Integer, ByVal y As Integer)
Me.DrawString(c, text, New Point(x, y))
End Sub
Public Sub DrawString(ByVal c As Control, ByVal text As String, _
ByVal pt As Point)
Me.DrawString(c, text, pt, c.ForeColor)
End Sub
Public Sub DrawString(ByVal c As Control, ByVal text As String, _
ByVal x As Integer, ByVal y As Integer, _
ByVal foreColor As Color)
Me.DrawString(c, text, New Point(x, y), foreColor)
End Sub
Public Sub DrawString(ByVal c As Control, ByVal text As String, _
ByVal pt As Point, ByVal foreColor As Color)
If c.IsHandleCreated Then
Using g = Graphics.FromHwnd(c.Handle)
Using f = New Font(Me._font, Me._font.Style)
Dim graphicsHandle As IntPtr = IntPtr.Zero
Dim fontHandle As IntPtr = IntPtr.Zero
Try
graphicsHandle = g.GetHdc()
fontHandle = KerningFont.SelectObject(graphicsHandle, _
f.ToHfont())
KerningFont.SetBackColor(graphicsHandle, _
ColorTranslator.ToWin32(c.BackColor))
KerningFont.SetTextColor(graphicsHandle, _
ColorTranslator.ToWin32(foreColor))
KerningFont.SetTextCharacterExtra(graphicsHandle, _
Me._kerning)
KerningFont.TextOut(graphicsHandle, pt.X + Me._kerning, _
pt.Y, text, text.Length)
Catch ex As Exception
'//suppress
Finally
KerningFont.DeleteObject(fontHandle)
g.ReleaseHdc()
End Try
End Using
End Using
End If
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Try
Me.Dispose(True)
Finally
GC.SuppressFinalize(Me)
End Try
End Sub
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
If Me._font IsNot Nothing Then
Me._font.Dispose()
Me._font = Nothing
End If
End If
End If
Me.disposedValue = True
End Sub
End Class