Export (0) Print
Expand All

DrawToolTipEventArgs Class

Provides data for the ToolTip.Draw event.


Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Public Class DrawToolTipEventArgs _
	Inherits EventArgs

The DrawToolTipEventArgs type exposes the following members.

Public methodDrawToolTipEventArgsInitializes a new instance of the DrawToolTipEventArgs class.

Public propertyAssociatedControlGets the control for which the ToolTip is being drawn.
Public propertyAssociatedWindowGets the window to which this ToolTip is bound.
Public propertyBoundsGets the size and location of the ToolTip to draw.
Public propertyFontGets the font used to draw the ToolTip.
Public propertyGraphicsGets the graphics surface used to draw the ToolTip.
Public propertyToolTipTextGets the text for the ToolTip that is being drawn.

Public methodDrawBackgroundDraws the background of the ToolTip using the system background color.
Public methodDrawBorderDraws the border of the ToolTip using the system border color.
Public methodDrawTextDraws the text of the ToolTip using the system text color and font.
Public methodDrawText(TextFormatFlags)Draws the text of the ToolTip using the system text color and font, and the specified text layout.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

The ToolTip.Draw event is raised by the ToolTip class when the ToolTip is drawn and the ToolTip.OwnerDraw property value is true. The handler for this event receives a parameter of type DrawToolTipEventArgs. The DrawToolTipEventArgs class contains all the information needed to paint the ToolTip, including the ToolTip text, the Rectangle, and the Graphics object on which the drawing should be done. To customize the look of the ToolTip, use the Rectangle to determine the bounds of the ToolTip, and the Graphics object to perform your customized drawing. You can increase the bounds of the ToolTip before it is shown by handling the Popup event.

DrawToolTipEventArgs also supports partial customization through the DrawBackground, DrawText and DrawBorder methods. Using these methods, you can owner draw portions of the ToolTip while leaving the other portions drawn in the standard way.

The following code example demonstrates how to custom draw the ToolTip. The example creates a ToolTip and associates it to three Button controls located on the Form. The example sets the OwnerDraw property to true and handles the Draw event. In the Draw event handler, the ToolTip is custom drawn differently depending on what button the ToolTip is being displayed for as indicated by the DrawToolTipEventArgs.AssociatedControl property.

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles

Public Module ToolTipExampleApp
    ' The main entry point for the application.
    <STAThread()> _
    Sub Main()
        Application.Run(New ToolTipExampleForm)
    End Sub 
End Module 

' Form for the ToolTip example. 
Public Class ToolTipExampleForm
    Inherits System.Windows.Forms.Form

    Private WithEvents toolTip1 As System.Windows.Forms.ToolTip
    Private WithEvents button1 As System.Windows.Forms.Button
    Private WithEvents button2 As System.Windows.Forms.Button
    Private WithEvents button3 As System.Windows.Forms.Button

    Public Sub New()
        ' Create the ToolTip and set initial values. 
        Me.toolTip1 = New System.Windows.Forms.ToolTip
        Me.toolTip1.AutoPopDelay = 5000
        Me.toolTip1.InitialDelay = 500
        Me.toolTip1.OwnerDraw = True 
        Me.toolTip1.ReshowDelay = 10

        ' Create button1 and set initial values. 
        Me.button1 = New System.Windows.Forms.Button
        Me.button1.Location = New System.Drawing.Point(8, 8)
        Me.button1.Text = "Button 1" 
        Me.toolTip1.SetToolTip(Me.button1, "Button1 tip text")

        ' Create button2 and set initial values. 
        Me.button2 = New System.Windows.Forms.Button
        Me.button2.Location = New System.Drawing.Point(8, 32)
        Me.button2.Text = "Button 2" 
        Me.toolTip1.SetToolTip(Me.button2, "Button2 tip text")

        ' Create button3 and set initial values. 
        Me.button3 = New System.Windows.Forms.Button
        Me.button3.Location = New System.Drawing.Point(8, 56)
        Me.button3.Text = "Button 3" 
        Me.toolTip1.SetToolTip(Me.button3, "Button3 tip text")

        ' Set up the Form. 
        Me.Controls.AddRange(New Control() {Me.button1, _
                                            Me.button2, _
        Me.Text = "owner drawn ToolTip example" 
    End Sub 

    ' Clean up any resources being used.         
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If (disposing) Then
        End If 

    End Sub 

    ' Determines the correct size for the button2 ToolTip. 
    Private Sub toolTip1_Popup(ByVal sender As System.Object, _
        ByVal e As PopupEventArgs) Handles toolTip1.Popup

        If e.AssociatedControl Is button2 Then 

            Dim f As New Font("Tahoma", 9)
                e.ToolTipSize = TextRenderer.MeasureText( _
                    toolTip1.GetToolTip(e.AssociatedControl), f)
            End Try 

        End If 
    End Sub 

    ' Handles drawing the ToolTip. 
    Private Sub toolTip1_Draw(ByVal sender As System.Object, _
        ByVal e As DrawToolTipEventArgs) Handles toolTip1.Draw
        ' Draw the ToolTip differently depending on which  
        ' control this ToolTip is for. 

        ' Draw a custom 3D border if the ToolTip is for button1. 
        If (e.AssociatedControl Is button1) Then 
            ' Draw the standard background.

            ' Draw the custom border to appear 3-dimensional.
            e.Graphics.DrawLines( _
                SystemPens.ControlLightLight, New Point() { _
                New Point(0, e.Bounds.Height - 1), _
                New Point(0, 0), _
                New Point(e.Bounds.Width - 1, 0)})
            e.Graphics.DrawLines( _
                SystemPens.ControlDarkDark, New Point() { _
                New Point(0, e.Bounds.Height - 1), _
                New Point(e.Bounds.Width - 1, e.Bounds.Height - 1), _
                New Point(e.Bounds.Width - 1, 0)})

            ' Specify custom text formatting flags. 
            Dim sf As TextFormatFlags = TextFormatFlags.VerticalCenter Or _
                                 TextFormatFlags.HorizontalCenter Or _

            ' Draw standard text with customized formatting options.
        ElseIf (e.AssociatedControl Is button2) Then 
            ' Draw a custom background and text if the ToolTip is for button2. 

            ' Draw the custom background.
            e.Graphics.FillRectangle(SystemBrushes.ActiveCaption, e.Bounds)

            ' Draw the standard border.

            ' Draw the custom text. 
            Dim sf As StringFormat = New StringFormat
                sf.Alignment = StringAlignment.Center
                sf.LineAlignment = StringAlignment.Center
                sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.None
                sf.FormatFlags = StringFormatFlags.NoWrap

                Dim f As Font = New Font("Tahoma", 9)
                    e.Graphics.DrawString(e.ToolTipText, f, _
                        SystemBrushes.ActiveCaptionText, _
                        RectangleF.op_Implicit(e.Bounds), sf)
                End Try 
            End Try 
        ElseIf (e.AssociatedControl Is button3) Then 
            ' Draw the ToolTip using default values if the ToolTip is for button3.
        End If 
    End Sub 
End Class

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
© 2014 Microsoft