【问题标题】:Kerning in vb.netvb.net 中的字距调整
【发布时间】:2010-01-27 10:05:25
【问题描述】:

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

【问题讨论】:

  • 你如何显示字符串?字距调整是字体的属性,而不是字符串,因此它完全取决于渲染它的系统。
  • 有点像字符间距
  • @S.Mark:相似,但不同。字距调整背后的想法是,它试图提高由符号本身的形状引起的视觉间隙的一致性,并且对于每对相邻的符号通常是不同的。出于这个原因,您需要一种算法来为您分配字距调整值或手动进行。也许OP只是在谈论跟踪(字符间距)。
  • 目前在程序中使用drawstring()来显示数据。我希望能够更改正在“绘制”的数据,以便字符之间的间距更大,从而更容易阅读。我不想改变字体大小,只是改变字符之间的水平间距。有什么想法吗?

标签: vb.net visual-studio typography kerning


【解决方案1】:

我发现的唯一方法是 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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • vb.net 中的 CSS?我不知道这是可能的,除了嵌入式浏览器控件。
    猜你喜欢
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 2015-06-07
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多