Search code examples
c#winformstooltip

How to set tooltip at current mouse location?


I registered Hotkey: Ctrl + Space. Hotkey message is sent to:

private void Hotkey_press()
{
... // I want to show tooltip "Hello" at current mouse location.
}

Is there any way to show this tooltip even the mouse doesnt point to any control and it is outside my Window.Form1?

Edit: That ToolTip can show even the form lost focus or hide


Solution

  • You want something like

    ToolTip tt = new ToolTip();
    IWin32Window win = this;
    tt.Show("String", win, mousePosition);
    

    Where the MousePosition can be obtained from the MouseEventArgs via

    private SomeMouseEventHandler(object sender, MouseEventArgs e)
    {
        System.Drawing.Point mousePosition = e.Location;
        ...
    }
    

    or using

    System.Drawing.Point mousePosition = Cursor.Position;
    

    also, you may want to set a longer duration for which the ToolTip is displayed, well just use the overloads available for the Show method, tt.Show("String", win, mousePosition, 5000); will display the tool tip for 5 seconds.

    I hope this helps.