在vb.net中进行Kerning

时间:2010-01-27 10:05:26

标签: vb.net visual-studio typography kerning

有谁知道如何更改vb.net中的字距调整(字符间的空格)?例如,我想将“STRING”更改为“S T R I N G”。如果可能的话,我希望能够创建自己的字体,我可以根据需要指定字距!提前谢谢!

2 个答案:

答案 0 :(得分:3)

我发现的唯一方法是P / Invoke。使用通用按钮确认通用表单时,此代码将起作用。

Imports System.Runtime.InteropServices

Public Class Form1
    Declare Function SetTextCharacterExtra Lib "gdi32" Alias "SetTextCharacterExtra" (ByVal hDC As Integer, ByVal nCharExtra As Integer) As Integer
    <DllImport("gdi32")> _
    Private Shared Function TextOut(ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal textstring As String, ByVal charCount As Integer) As Boolean
    End Function
    <DllImport("gdi32")> _
    Private Shared Function SelectObject(ByVal hdc As IntPtr, ByVal hgdiobj As IntPtr) As IntPtr
    End Function
    <DllImport("gdi32")> _
    Private Shared Function DeleteObject(ByVal objectHandle As IntPtr) As Boolean
    End Function
    <DllImport("gdi32")> _
    Private Shared Function SetBkColor(ByVal hdc As IntPtr, ByVal crColor As Integer) As UInt32
    End Function
    <DllImport("gdi32")> _
    Private Shared Function SetTextColor(ByVal hdc As IntPtr, ByVal crColor As Integer) As UInt32
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using G = Graphics.FromHwnd(Me.Handle)
            Using myFont As New System.Drawing.Font("Arial", 20, FontStyle.Regular, GraphicsUnit.Pixel)
                'Regular Way
                Dim LeftEdge = 20
                G.DrawString("Hello", myFont, Brushes.Red, LeftEdge, 40)

                'If you want kerning
                Dim Kerning As Integer = 6 'I think this is twips
                Dim Hdc As IntPtr
                Dim FontPtr As IntPtr
                Try
                    'Grab the Graphic object's handle
                    Hdc = G.GetHdc()
                    'Set the current GDI font
                    FontPtr = SelectObject(Hdc, myFont.ToHfont())
                    'Set the drawing surface background color
                    SetBkColor(Hdc, ColorTranslator.ToWin32(Me.BackColor))
                    'Set the text color
                    SetTextColor(Hdc, ColorTranslator.ToWin32(Color.Red))
                    'Set the kerning
                    SetTextCharacterExtra(Hdc, Kerning)
                    Dim Text = "Hello"
                    'Draw the text at (20,60), Kerning will be applied so reset the left edge to half of kerning
                    TextOut(Hdc, LeftEdge + (Kerning \ 2), 60, Text, Text.Length)
                Catch ex As Exception

                Finally
                    'Release the font
                    DeleteObject(FontPtr)
                    'Release the handle on the graphics object
                    G.ReleaseHdc()
                End Try
            End Using
        End Using
    End Sub
End Class

答案 1 :(得分:-3)

您可以使用CSS。 我通常在我的aspx页面中设置letter-spacing样式属性。