.net – 更改工具提示字体

我需要一个带有自定义Font的工具提示.

我有以下代码,这可行…但工具提示大小不适合文本.

错误在哪里?

Public Class KeolisTooltip
  Inherits ToolTip

  Sub New()
    MyBase.New()
    Me.OwnerDraw = True
    AddHandler Me.Draw, AddressOf OnDraw
  End Sub

  Private _Font As Font
  Public Property Font() As Font
    Get
      Return _Font
    End Get
    Set(ByVal value As Font)
      _Font = value
    End Set
  End Property

  Public Sub New(ByVal Cont As System.ComponentModel.IContainer)
    MyBase.New(Cont)
    Me.OwnerDraw = True
    AddHandler Me.Draw, AddressOf OnDraw
  End Sub


  Private Sub OnDraw(ByVal sender As Object, ByVal e As DrawToolTipEventArgs)
    Dim newArgs As DrawToolTipEventArgs

    If _Font Is Nothing Then
      newArgs = e
    Else
      Dim newSize As Size = Size.Round(e.Graphics.MeasureString(e.ToolTipText, Me._Font))
      Dim newBounds As New Rectangle(e.Bounds.Location, newSize)

      newArgs = New DrawToolTipEventArgs( _
         e.Graphics, _
         e.AssociatedWindow, _
         e.AssociatedControl, _
         newBounds, _
         e.ToolTipText, _
         Me.BackColor, _
         Me.ForeColor, _
         Me._Font)
    End If

    newArgs.DrawBackground()
    newArgs.DrawBorder()
    newArgs.DrawText()
  End Sub

End Class

最佳答案
Size.Round(来自MSDN页面)

Converts the specified SizeF structure to a Size structure by rounding the values of the SizeF structure to the nearest integer values.

(我的重点).

因此,如果

e.Graphics.MeasureString(e.ToolTipText, Me._Font)

产生23.4和42.1(例如)的值然后它们将分别舍入到23和42,因此您的工具提示将略微过小.

点赞