首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改ToolTip字体

更改ToolTip字体
EN

Stack Overflow用户
提问于 2010-08-20 23:56:46
回答 2查看 3.3K关注 0票数 4

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

我有下面的代码,这是可以工作的。但工具提示大小不适合文本。

错误在哪里?

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2010-08-25 06:39:21

Size.Round (从MSDN页面)

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

(我的重点)。

因此,如果

代码语言:javascript
复制
e.Graphics.MeasureString(e.ToolTipText, Me._Font)

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

票数 2
EN

Stack Overflow用户

发布于 2010-09-01 14:11:50

除了OnDraw事件之外,您是否可以尝试在OnResize事件上添加调整大小的逻辑?我认为您将在该事件上获得正确的值。只要试着让它知道它是否有效。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3532590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档