简体   繁体   English

如何使ToolTip跟随鼠标?

[英]How to make the ToolTip follow the mouse?

I want the ToolTip to follow my mouse moving over one control. 我希望工具提示跟随我的鼠标在一个控件上移动。 For example, let's take a panel. 例如,让我们进行一个小组讨论。 When mouse location is inside the Rectangle(100, 100, 50, 50) I want ToolTip to be visible and always on the right down of the mouse. 当鼠标位置位于Rectangle(100,100,50,50)内部时,我希望ToolTip可见并且始终在鼠标的右下方。 When it's outside this rectangle, I want ToolTip to be invisible. 当它在此矩形之外时,我希望工具提示不可见。

I tried to do this like that: 我试图这样做:

ToolTip toolTip = new ToolTip();
int x, y;

protected override void OnMouseMove(MouseEventArgs e)
{
      if ((x == e.X) && (y == e.Y) && (new Rectangle(100, 100, 50, 50).Contains(e.Location))
          toolTip.Show("some text", this, x + 10, y + 10);
      else
      {
          x = e.X;
          y = e.Y;
          toolTip.Hide(this);
      }
}

But there's a problem - when my toolTip shows up - it gets the focus and after that OnMouseMove(MouseEventArgs e) doesn't work any more. 但是有一个问题-当我的toolTip出现时-它得到了焦点,然后OnMouseMove(MouseEventArgs e)不再起作用。 I tried to get the focus to the panel in the end of that function, but it doesn't work. 我试图将焦点放在该功能结尾处的面板上,但是它不起作用。 I also tried some tricks with OnMouseHover, but it was the same effect. 我还尝试了OnMouseHover的一些技巧,但效果相同。

Don't use a ToolTip for that - if the Panel is drawn on, draw your own ToolTip; 请勿为此使用工具提示-如果绘制了面板,请绘制自己的工具提示; otherwise, use a Panel and respond to MouseMove events from both, but ignore e.Location and instead use System.Windows.Forms.Cursor.Position and PointToClient . 否则,请使用Panel并从两者响应MouseMove事件,但忽略e.Location ,而是使用System.Windows.Forms.Cursor.PositionPointToClient

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM