问题
I'm wondering if it is possible somehow locate popup of ToolTip outside of application form in the fixed point over the empty desktop with MouseHover event, of course if event is useful for ToolTip, not sure. Or any other way if it is possible
I'm not asking for how to display another form as an option for this goal.
回答1:
You can use either of these options:
Handle showing and hiding the
ToolTip
yourself. You can useMouseHover
show theToolTip
in desired location and usingMouseLeave
hide it.Using MoveWindow Windows API method, force the tooltip to show in a specific location instead of default location.
Option 1
You can handle MouseHover
and MouseLeave
event of your control(s) and show ToolTip
in specific location of desktop window this way:
private void control_MouseHover(object sender, EventArgs e)
{
var control = (Control)sender;
var text = toolTip1.GetToolTip(control);
if (!string.IsNullOrEmpty(text))
toolTip1.Show(text, control, control.PointToClient(new Point(100, 100)));
}
private void control_MouseLeave(object sender, EventArgs e)
{
var control = (Control)sender;
toolTip1.Hide(control);
}
Option 2
As another option which I previously offered for align right edges of a control and ToolTip, you can set OwnerDraw property of ToolTip
to true
and handle Draw
event of the control and use MoveWindow
Windows API method to move ToolTip
to desired location:
[System.Runtime.InteropServices.DllImport("User32.dll")]
static extern bool MoveWindow(IntPtr h, int x, int y, int width, int height, bool redraw);
private void toolTip1_Draw(object sender, DrawToolTipEventArgs e) {
e.DrawBackground();
e.DrawBorder();
e.DrawText();
var t = (ToolTip)sender;
var h = t.GetType().GetProperty("Handle",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var handle = (IntPtr)h.GetValue(t);
var location = new Point(100,100);
MoveWindow(handle, location.X, location.Y, e.Bounds.Width, e.Bounds.Height, false);
}
回答2:
It sounds like ultimately what you want is a box to display some information whenever you hover over some particular items on your GUI. You also say that you want the information to display at a fixed point.
As opposed to achieving this with the tool-tip, I would do the following:
- Create some fixed label or text-box to display information and put it somewhere on your Windows form.
- Create a subscriber to the mouse hover event.
- Based on the
sender
(which control you're hovering) from the mouse hover event, choose what information to display in the fixed location.
I've seen people doing this in some other programs... Take, RealTerm for example. Try it out if you want and see how it feels before you try this solution.
On the other hand, if you must do this with a tool-tip. You can choose the position using overloads of ToolTip.Show.
来源:https://stackoverflow.com/questions/42535141/winform-tooltip-location-setting