User drawn ToolStrip Menu ToolTips

Frank Brady 21 Reputation points
2023-01-20T16:41:05.3733333+00:00

With considerable help from this forum, I have recently completed conversion of my vb.net applications to use user draw for controls. However, I have had no luck asking how to extend the user draw property to ToolTip of ToolstripMenuItems.

And perhaps the first question I need to ask is what "tag" do I need to use to direct my question to the proper area of this forum?

Thanks

frankbrady-5895

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,827 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
{count} votes

Accepted answer
  1. LesHay 7,126 Reputation points
    2023-01-20T20:17:36.93+00:00

    Hi

    As I said previously, I don't know if/how ToolTips on a menu can be modified. The only thing I would try if faced with the same issue would be:

    1. Add a new Form to the project. Set it to a suitable size.
    2. In the parent form menu, design a mouse hover sub to handle all the items that need a ToolTip and set the appropriate text to show.
    3. Show the new Form which has a Paint event handler to draw the appropriate text with a font/size/colour as needed, and a mouse leave event handler to close the form when pointer moved out (or, set a Timer to close after a set time)
    4. Once tested as is, the pop up form can be made borderless.
    5. If all of that is OK, things like making fonts/colours/background etc variable to suit.

    Here is some very basic code to try (as a test project) It works fine here (although, not as configurable as an Owner Drawn tooltip.

    Main Form

      Private Sub ToolStripOpacityMenu_MouseHover(sender As Object, e As EventArgs) Handles ToolStripOpacityMenu.MouseHover
        Dim f3 As New Form3
        f3.txt = "This is a test"
        f3.Show()
      End Sub
    
    

    Nee Form

    Public Class Form3
      Public Property txt As String
      Private Sub Form3_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
        Dim fnt As New Font("Arial", 24)
        Using b As New SolidBrush(Color.Red)
          e.Graphics.DrawString(txt, fnt, b, New Point(8, 10))
        End Using
        Cursor.Position = New Point(Left + 80, Top + 60)
      End Sub
      Private Sub Form3_MouseLeave(sender As Object, e As EventArgs) Handles Me.MouseLeave
        Close()
      End Sub
    End Class
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Reza Aghaei 4,936 Reputation points MVP
    2023-02-01T17:58:35.1833333+00:00

    A little late to the party 😄, but I'd like to share the right way of customizing the ToolTip of ToolStrip, MenuStrip, ContextMenuStrip, or anything deriving from ToolStrip.

    ToolStrip has an internal ToolTip property that you can find using reflection, and then treat it like any normal tooltip, for example you can set its forecolor, backcolor, or change the font and size of it and have a custom logic for drawing the tooltip.

    Look at the following customizations; it's a tooltip with White text, Red backcolor, and twice size/font as normal:

    tooltip

    And here is the code that I used for this example:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            SetupCustomDraw(MenuStrip1)
            SetupCustomDraw(FileToolStripMenuItem.DropDown)
            SetupCustomDraw(ToolStrip1)
        End Sub
        Sub SetupCustomDraw(MyToolStrip As ToolStrip)
            Dim MyField = MyToolStrip.GetType().GetProperty("ToolTip",
                Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
            Dim MyToolTip = CType(MyField.GetValue(MyToolStrip), ToolTip)
    
            MyToolTip.OwnerDraw = True
            MyToolTip.BackColor = Color.Red
            MyToolTip.ForeColor = Color.White
            AddHandler MyToolTip.Popup,
                Sub(obj, args)
                    args.ToolTipSize = New Size(args.ToolTipSize.Width * 2, args.ToolTipSize.Height * 2)
                End Sub
            AddHandler MyToolTip.Draw,
                Sub(obj, args)
                    Dim F = New Font(MyToolStrip.Font.FontFamily, MyToolStrip.Font.Size * 2)
                    args.DrawBackground()
                    TextRenderer.DrawText(args.Graphics, args.ToolTipText,
                        F, args.Bounds, MyToolTip.ForeColor)
                    args.DrawBorder()
                End Sub
        End Sub
    End Class
    
    

    tooltip2

    2 people found this answer helpful.