Hello everyone, I am attempting to make a custom control similar to a tooltip however I am not sure exactly how to produce the effect I desire. My goal is to have a popup, which is shown when the mouse hovers over a control, that contains some message and is shown at the top center of the hovered control with a triangle "pointer" overlapping onto the control (See illustration).
e42e24cd2b1615ad81ffe8932e7d2467

I would like the popup to automatically be sized to the text contained within padded by a few pixels; I know this is possible using the Graphics.MeasureString() function. I already have code that round the corners of rectangles.

I have attempted to write this code within the control that will have the popup itself, however you are unable to draw beyond the bounds of the control which I need to do in this circumstance. I have experimented with the idea of inheriting the ToolTip control, however this does not provide enough control to draw everything I need (at least I think). I know that it may be possible to inherit a ToolStripDropDown, however I am not sure how to use it in this case.

Any help is greatly appreciated. Here is some of my preliminary code:

Dim pointerDimensions As Size = New Size(10, 10)
        Dim bodyBrush As Brush = Brushes.SaddleBrown

        stringDimensions = e.Graphics.MeasureString(e.ToolTipText, e.Font)
        Dim Width As Integer = stringDimensions.Width + 10
        Dim Height As Integer = stringDimensions.Height + 5 + pointerDimensions.Height

        e.Graphics.Clear(Color.Transparent)

        e.Graphics.FillPath(bodyBrush, Shape.RoundedRectangle(New Rectangle(0, 0, Width, Height - pointerDimensions.Height), 3, Shape.Corner.None))
        e.Graphics.DrawString(message, e.Font, Brushes.Black, New Point(5, 5))

        Dim triangleTop As Integer = Height - pointerDimensions.Height
        Dim triangleCenter As Integer = Width / 2
        e.Graphics.FillPolygon(bodyBrush, {New Point(triangleCenter + (pointerDimensions.Width / 2), triangleTop), _
                                           New Point(triangleCenter - (pointerDimensions.Width / 2), triangleTop), _
                                           New Point(triangleCenter, triangleTop + pointerDimensions.Height)})

This is just for testing and is definatly not complete, pretty, of 100% working.

Thanks alot,
Patrick

Have you thought of a custom Label? Just use the MouseEnter & MouseLeave events, or a timer could close it.

This article has been dead for over six months. Start a new discussion instead.