2010-08-20 46 views
4

我需要提供自定义字体的工具提示。更改工具提示字体

我有下面的代码,这个工程...但工具提示大小不适合文本。

错误在哪里?

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 
+0

我没有看到你实际实例化字体的位置。 – JustBoo 2010-08-20 17:09:02

+0

@JustBoo:在代码中注释“If _Font Is Nothing Then” – serhio 2010-08-28 14:29:07

回答

2

Size.Round(从MSDN页)

通过舍入所述的SizeF结构的值到最接近整数值指定的SizeF结构的尺寸结构转换。

(我强调)。

因此,如果

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

生产的23.4和42.1(说),那么他们将被四舍五入为23和42分别值,所以你的提示将略有太小。

+0

母猪,你提出了什么解决方案? – serhio 2010-08-28 14:28:00

+0

@serhio - 您需要舍入到下一个最高整数(或者甚至添加更多的填充)。 – ChrisF 2010-08-29 12:57:44

0

除了OnDraw事件之外,您是否可以尝试在OnResize事件上添加调整大小逻辑?我想你会在那件事上得到正确的价值。试着让它知道它是否有效。