Tooltips

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
I have a form with no controls, but with lots of circles drawn on it. I would like to be able to display a tooltip when the mouse is moved over any of the circles.

This code doesnt work properly, but it gives an idea of what I am trying to achieve.

VB.NET:
    Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        With e.Graphics
            .TranslateTransform(50, 50)
            .ScaleTransform(2.3, 2.3)
            'DrawCircles()
        End With
    End Sub

    Private Sub frmMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        Dim tt As New ToolTip
        If e.Location.X >= 0 AndAlso e.Location.X <= 50 AndAlso e.Location.Y >= 0 AndAlso e.Location.Y <= 50 Then
            tt.SetToolTip(CType(sender, System.Windows.Forms.Control), "Top left!!")
        End If
    End Sub

The user can zoom in on the circles, so my plan is to keep track of the circle location and sizes at all times.

Screenshot for information

Any pointers / suggestions would be greatly appreciated :D
 
The user can zoom in on the circles, so my plan is to keep track of the circle location and sizes at all times.

Any pointers / suggestions would be greatly appreciated :D
Location+Size=Rectangle. Rectangle has a Contains method that takes a Point as parameter, this makes hit-testing much easier. If you need more accurate hit-test for non-rectangular objects (which I don't think you need) you can use the GraphicsPath and its IsVisible method to do the same.

Since you're working with a single control the MouseHover event doesn't do any good, but would be preferred if it did, but you can do the same with a Timer. This will save a lot of processing as the mouse moves. For example set timer for hover interval like 1sec and send Start command each mouse move, this will reset the Timer each time. In Tick event stop Timer and do the hittest. Now instead of possibly thousands of pointless calculations each second you only get the one that really matters.
 
Thanks John, that gave me the direction I required.

I was a little confused for a while until I realised that when the Tooltip is displayed, it raises an additional MouseMove event. That just caused me to create and draw my own GDI tooltip, which works perfectly :)
 
Back
Top