【问题标题】:Change ToolTip font更改工具提示字体
【发布时间】:2010-08-20 15:56:46
【问题描述】:

我需要一个带有自定义字体的工具提示。

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

哪里出错了?

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

【问题讨论】:

  • 我看不到您实际实例化字体的位置。
  • @JustBoo:在代码中备注“If _Font Is Nothing Then”

标签: .net winforms tooltip


【解决方案1】:

Size.Round(来自 MSDN 页面)

通过将 SizeF 结构的值四舍五入为最近整数值,将指定的 SizeF 结构转换为 Size 结构。

(我的重点)。

因此,如果

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

产生 23.4 和 42.1(比如说)的值,然后它们将分别四舍五入为 23 和 42,因此您的工具提示会稍微太小。

【讨论】:

  • sow,你有什么解决方案?
  • @serhio - 你需要四舍五入到下一个最大的整数(或者甚至添加更多的填充)。
【解决方案2】:

除了 OnDraw 事件之外,您能否尝试在 OnResize 事件上添加调整大小的逻辑?我认为您将在该事件中获得正确的值。试试看它是否有效。

【讨论】:

    猜你喜欢
    • 2021-09-03
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 2014-07-04
    • 2015-01-03
    相关资源
    最近更新 更多