I created a custom UserControl that displays itself in a ToolStripDropDown to emulate ToolTip functionality. It mostly works fine for any Control when I subscribe to their MouseEnter and MouseLeave events.
I want to also use it for custom objects (not Controls). I’ve created an interface that defines MouseEnter and MouseLeave events so that I can subscribe any object (such as custom-drawn primitives) to this tooltip. These classes do their own work to determine when to trigger MouseEnter and MouseLeave.
My problem is that when the tooltip is shown, my UserControls which contain the custom objects do not receive MouseMove events, even though the Tooltip is being shown off to the side and not under the mouse. I am generating my own MouseLeave event based on checking in MouseMove if the mouse is no longer over the object in question. But obviously without MouseMove events, MouseLeave never fires.
When I show the tooltip on a Control, the same thing happens (no MouseMove events) except that MouseLeave still fires.
1) How can I emulate this MouseLeave functionality? Do I have to use p/invoke to SetCapture mouse movement, or does anyone know an easier way?
2) When the tooltip shows, even though neither the ToolStripDropDown or my UserControl inside it fire a “GotFocus” event, I still lose keyboard focus as long as the tooltip is shown which is also not desirable tooltip behavior. Can I avoid that?
Basically I want it to be a completely non-focusable, non-interfering tooltip. I have looked at a sample project called SuperTooltip but it had the same flawed functionality. I have tried setting ControlStyles.Selectable to false and did not notice any change.
Here is the code where I create my tooltip UserControl:
public CustomTooltip()
{
this.SetStyle(ControlStyles.Selectable, false);
dropDown = new ToolStripDropDown();
dropDown.AutoSize = false;
dropDown.Margin = Padding.Empty;
dropDown.Padding = Padding.Empty;
host = new ToolStripControlHost(this);
host.AutoSize = false;
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
this.Location = new Point(0, 0);
dropDown.Items.Add(host);
}
And I show it with:
dropDown.Show(
new Point(Cursor.Position.X, Cursor.Position.Y + Cursor.Current.Size.Height),
ToolStripDropDownDirection.BelowRight
);
I found I was able to accomplish this by deriving from Form and not using ToolStripDropDown at all. This class emulates a tooltip’s functionality and allows custom fade in/fade out parameters. You can either subscribe a control or any class that defines and implements ITooltipTarget for MouseEnter and MouseLeave.
To use the above classes, you just need to derive from CustomTooltip to make your own custom drawn tooltip. The derived class would use the Tag property to determine the content displayed. For example, if I want a tooltip that associates an Image with an object and draws that image, I’d do something like:
And in order to use this CustomImageTooltip class in your application, you’d need to just subscribe and unsubscribe to a single instance of the class: