C# 如何在当前鼠标位置设置工具提示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11885785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:38:03  来源:igfitidea点击:

How to set tooltip at current mouse location?

c#winformstooltip

提问by Tuyen Pham

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

我注册了 Hotkey: Ctrl+ Space。热键消息发送至:

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?

有没有办法显示这个工具提示,即使鼠标没有指向任何控件并且它在我的 Window.Form1 之外?

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

编辑:该工具提示甚至可以显示失去焦点或隐藏的表单

采纳答案by MoonKnight

You want something like

你想要类似的东西

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

Where the MousePositioncan be obtained from the MouseEventArgsvia

MousePosition可以从获得MouseEventArgs通过

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 ToolTipis displayed, well just use the overloads available for the Showmethod, tt.Show("String", win, mousePosition, 5000);will display the tool tip for 5 seconds.

此外,您可能希望设置更长的ToolTip显示持续时间,只需使用该Show方法可用的重载,tt.Show("String", win, mousePosition, 5000);将显示工具提示 5 秒。

I hope this helps.

我希望这有帮助。

回答by Setsu

As this answersuggests, there is no managed way to accomplish this. If you want to show a tool tip control when your program is not in focus then the "right" way to do it is to PInvoke Win32 and use CreateWindowEx. The answer linked above given by gideon shows some pointers on how to do it, but nonetheless it is very complicated.

正如这个答案所暗示的那样,没有管理的方法来实现这一点。如果您想在程序未处于焦点时显示工具提示控件,那么“正确”的方法是 PInvoke Win32 并使用CreateWindowEx. gideon 给出的上面链接的答案显示了一些关于如何做到这一点的指示,但它仍然非常复杂。

If you don't mind using thrid party libraries, AutoItprovides a way to create tool tips easily without having to deal with Win32 yourself.

如果您不介意使用第三方库,AutoIt提供了一种无需自己处理 Win32 即可轻松创建工具提示的方法。

Here is a very simple example demonstrating use:

这是一个非常简单的示例,演示了用法:

//make sure AutoItX3.dll is referenced in your project
using AutoItX3Lib;

private AutoItX3 myAutoIt = new AutoItX3();

private async void ShowToolTipAtMouse(string message)
{
    //default position is bottom right of mouse pointer,
    //but you can set the x and y positions yourself
    myAutoIt.ToolTip(message);

    //call the function again with an empty argument to close
    await Task.Delay(1000);
    myAutoIt.ToolTip(String.Empty);
}

This will work as long as your program is running; doesn't matter if it is in/out of focus or even hidden. Downside is you don't get the regular fade out animation (it just vanishes). Also, if you need multiple tool tips at once you need to have multiple AutoItX3objects.

只要您的程序正在运行,这就会起作用;它是否在焦点内/焦点外或什至隐藏都无关紧要。缺点是您没有获得常规的淡出动画(它只是消失了)。此外,如果您一次需要多个工具提示,则需要有多个AutoItX3对象。