How to make tooltips on non-Controls ?

T

trant

Would it be possible to use the existing Tooltip functionality on non-Control
objects?

Say I have several boxes drawn across the background for a Panel, and if the
user hovers the mouse over one of the shapes a certain tooltip specific to
that shape shows up, and likewise for each shape.

If I cannot use Tooltip this way, any suggestions on how I can accomplish
this task?

I can find the right shape with mouse coord hit testing in Mouse Hover but I
somehow need to incorporate a timer in there which resets every time the
mouse moves - this sounds like something that would be a potential
performance hit if not done properly.

Only idea I have is perhaps on MouseHover start a Timer which when elapses
in a few seconds would trigger a tooltip to show up at the current xy. But if
they moved the mouse again this timer would be reset. Is that right?
 
P

Peter Duniho

trant said:
Would it be possible to use the existing Tooltip functionality on non-Control
objects?

I don't think so. You have to have an instance of Control to pass to
the SetToolTip() method. The ToolTip class then uses that instance to
track mouse events related to the Control.
Say I have several boxes drawn across the background for a Panel, and if the
user hovers the mouse over one of the shapes a certain tooltip specific to
that shape shows up, and likewise for each shape.

If I cannot use Tooltip this way, any suggestions on how I can accomplish
this task? [...]

From the rest of your post, it seems like you already have a decent
idea of how you would. The trickiest part is (IMHO) detecting the cases
where you are not going to get another mouse event for the object (e.g.
because the application was task-switched out), but you still should not
be showing the tooltip (i.e. you need to stop the timer and abort the
countdown to showing the tooltip).

In terms of the timer itself, I don't think that there should actually
be a significant problem continually resetting the timer as the mouse is
moved. But, if for some reason you find it's causing an issue, it might
be slightly more efficient to have your own dedicated timer thread,
using (for example) a Monitor.Wait() call with a timeout for timing, and
a Monitor.Pulse() call to wake up the thread before the timeout if the
timer needs to be reset. The Monitor class is built into .NET and is
somewhat more efficient to use than other Win32-based objects, such as a
System.Windows.Forms.Timer or System.Threading.AutoResetEvent.

Pete
 
T

trant

Thank you Pete!

And thanks for warning me about those cases where the tooltips should not
show up - I had not thought of that!

Peter Duniho said:
trant said:
Would it be possible to use the existing Tooltip functionality on non-Control
objects?

I don't think so. You have to have an instance of Control to pass to
the SetToolTip() method. The ToolTip class then uses that instance to
track mouse events related to the Control.
Say I have several boxes drawn across the background for a Panel, and if the
user hovers the mouse over one of the shapes a certain tooltip specific to
that shape shows up, and likewise for each shape.

If I cannot use Tooltip this way, any suggestions on how I can accomplish
this task? [...]

From the rest of your post, it seems like you already have a decent
idea of how you would. The trickiest part is (IMHO) detecting the cases
where you are not going to get another mouse event for the object (e.g.
because the application was task-switched out), but you still should not
be showing the tooltip (i.e. you need to stop the timer and abort the
countdown to showing the tooltip).

In terms of the timer itself, I don't think that there should actually
be a significant problem continually resetting the timer as the mouse is
moved. But, if for some reason you find it's causing an issue, it might
be slightly more efficient to have your own dedicated timer thread,
using (for example) a Monitor.Wait() call with a timeout for timing, and
a Monitor.Pulse() call to wake up the thread before the timeout if the
timer needs to be reset. The Monitor class is built into .NET and is
somewhat more efficient to use than other Win32-based objects, such as a
System.Windows.Forms.Timer or System.Threading.AutoResetEvent.

Pete
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top