Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winform ToolTip location setting

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.

like image 761
nikorio Avatar asked Apr 07 '26 13:04

nikorio


1 Answers

You can use either of these options:

  1. Handle showing and hiding the ToolTip yourself. You can use MouseHover show the ToolTip in desired location and using MouseLeave hide it.

  2. 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);
}
like image 65
Reza Aghaei Avatar answered Apr 09 '26 03:04

Reza Aghaei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!