开发者

How to make the ToolTip follow the mouse?

开发者 https://www.devze.com 2023-04-01 03:42 出处:网络
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 o

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 righ开发者_运维百科t down of the mouse. 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. 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号