Results 1 to 2 of 2

Thread: [.NET 3.5+] KerningFont Class

  1. #1

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    [.NET 3.5+] KerningFont Class

    I created this class for this thread, maybe it will help someone else too.

    Usage:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Paint(ByVal sender As Object, _
    4.                             ByVal e As System.Windows.Forms.PaintEventArgs) _
    5.                             Handles Me.Paint
    6.  
    7.         Using kerningFont = New KerningFont(New Font("Calibri", 10.0F), 2)
    8.  
    9.             '//draw your kerning font
    10.             kerningFont.DrawString(Me, "This is a test string demonstrating kerning!", _
    11.                              10, 10)
    12.             '//change kerning
    13.             kerningFont.Kerning = 6
    14.  
    15.             '//draw another kerning font
    16.             kerningFont.DrawString(Me, "This is a test string demonstrating kerning!", _
    17.                                    10, 30)
    18.  
    19.         End Using
    20.  
    21.     End Sub
    22.  
    23. End Class

    Class:
    vb.net Code:
    1. Public Class KerningFont
    2.     Implements IDisposable
    3.  
    4.     '//APIs
    5.     <System.Runtime.InteropServices.DllImport("gdi32")> _
    6.     Protected Shared Function SelectObject(ByVal handle As IntPtr, _
    7.                                            ByVal hgdiobj As IntPtr) As IntPtr
    8.     End Function
    9.  
    10.     <System.Runtime.InteropServices.DllImport("gdi32")> _
    11.     Protected Shared Function SetTextCharacterExtra(ByVal handle As IntPtr, _
    12.                                                     ByVal nCharExtra As Integer) _
    13.                                                     As Integer
    14.     End Function
    15.  
    16.     <System.Runtime.InteropServices.DllImport("gdi32")> _
    17.     Protected Shared Function TextOut(ByVal handle As IntPtr, ByVal x As Integer, _
    18.                                       ByVal y As Integer, ByVal text As String, _
    19.                                       ByVal charCount As Integer) As Boolean
    20.     End Function
    21.  
    22.     <System.Runtime.InteropServices.DllImport("gdi32", EntryPoint:="SetBkColor")> _
    23.     Protected Shared Function SetBackColor(ByVal handle As IntPtr, _
    24.                                            ByVal color As Integer) As UInt32
    25.     End Function
    26.  
    27.     <System.Runtime.InteropServices.DllImport("gdi32")> _
    28.     Protected Shared Function DeleteObject(ByVal handle As IntPtr) As Boolean
    29.     End Function
    30.  
    31.     <System.Runtime.InteropServices.DllImport("gdi32")> _
    32.     Protected Shared Function SetTextColor(ByVal handle As IntPtr, _
    33.                                            ByVal color As Integer) As UInt32
    34.     End Function
    35.  
    36.     '//fields
    37.     Private disposedValue As Boolean = False
    38.  
    39.     '//properties
    40.     Private _font As Font = Nothing
    41.     Public ReadOnly Property Font() As Font
    42.         Get
    43.             Return Me._font
    44.         End Get
    45.     End Property
    46.  
    47.     Private _kerning As Integer = 0
    48.     Public Property Kerning() As Integer
    49.         Get
    50.             Return Me._kerning
    51.         End Get
    52.         Set(ByVal value As Integer)
    53.             Me._kerning = value
    54.         End Set
    55.     End Property
    56.  
    57.     '//constructors
    58.     Public Sub New(ByVal f As Font, _
    59.                    ByVal kerning As Integer)
    60.         Me._kerning = kerning
    61.         Me._font = f
    62.     End Sub
    63.  
    64.     '//methods
    65.     Public Sub DrawString(ByVal c As Control, ByVal text As String, _
    66.                           ByVal x As Integer, ByVal y As Integer)
    67.         Me.DrawString(c, text, New Point(x, y))
    68.     End Sub
    69.  
    70.     Public Sub DrawString(ByVal c As Control, ByVal text As String, _
    71.                           ByVal pt As Point)
    72.         Me.DrawString(c, text, pt, c.ForeColor)
    73.     End Sub
    74.  
    75.     Public Sub DrawString(ByVal c As Control, ByVal text As String, _
    76.                           ByVal x As Integer, ByVal y As Integer, _
    77.                           ByVal foreColor As Color)
    78.         Me.DrawString(c, text, New Point(x, y), foreColor)
    79.     End Sub
    80.  
    81.     Public Sub DrawString(ByVal c As Control, ByVal text As String, _
    82.                           ByVal pt As Point, ByVal foreColor As Color)
    83.         If c.IsHandleCreated Then
    84.             Using g = Graphics.FromHwnd(c.Handle)
    85.                 Using f = New Font(Me._font, Me._font.Style)
    86.                     Dim graphicsHandle As IntPtr = IntPtr.Zero
    87.                     Dim fontHandle As IntPtr = IntPtr.Zero
    88.                     Try
    89.                         graphicsHandle = g.GetHdc()
    90.                         fontHandle = KerningFont.SelectObject(graphicsHandle, _
    91.                                                               f.ToHfont())
    92.                         KerningFont.SetBackColor(graphicsHandle, _
    93.                                                  ColorTranslator.ToWin32(c.BackColor))
    94.                         KerningFont.SetTextColor(graphicsHandle, _
    95.                                                  ColorTranslator.ToWin32(foreColor))
    96.                         KerningFont.SetTextCharacterExtra(graphicsHandle, _
    97.                                                           Me._kerning)
    98.                         KerningFont.TextOut(graphicsHandle, pt.X + Me._kerning, _
    99.                                             pt.Y, text, text.Length)
    100.                     Catch ex As Exception
    101.                         '//suppress
    102.                     Finally
    103.                         KerningFont.DeleteObject(fontHandle)
    104.                         g.ReleaseHdc()
    105.                     End Try
    106.                 End Using
    107.             End Using
    108.         End If
    109.     End Sub
    110.  
    111.     Public Sub Dispose() Implements IDisposable.Dispose
    112.         Try
    113.             Me.Dispose(True)
    114.         Finally
    115.             GC.SuppressFinalize(Me)
    116.         End Try
    117.     End Sub
    118.  
    119.     Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    120.         If Not Me.disposedValue Then
    121.             If disposing Then
    122.                 If Me._font IsNot Nothing Then
    123.                     Me._font.Dispose()
    124.                     Me._font = Nothing
    125.                 End If
    126.             End If
    127.         End If
    128.         Me.disposedValue = True
    129.     End Sub
    130.  
    131. End Class
    Attached Images Attached Images  

  2. #2

    Re: [.NET 3.5+] KerningFont Class

    Looks like some really interesting code there FA. It looks pretty nice, I'll have to check it out.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width