C# 如何在windows窗体中检查光标位置是否在图表控件之外?
我正在C# 如何在windows窗体中检查光标位置是否在图表控件之外?,c#,winforms,mouseevent,cursor-position,C#,Winforms,Mouseevent,Cursor Position,我正在MS图表中显示工具提示。从图表控件移动到其他控件或窗体可用空间时,工具提示不会隐藏 如何在windows窗体中检查光标位置是否在图表控件之外 我尝试了下面的代码,它不适合我 private void Form1_MouseMove(object sender, MouseEventArgs e) { if (!chart.ClientRectangle.Contains(chart.PointToClient(new Point(e.X,e.Y)))) {
MS图表中显示工具提示
。从图表控件移动到其他控件或窗体可用空间时,工具提示不会隐藏
如何在windows窗体中检查光标位置是否在图表控件之外
我尝试了下面的代码,它不适合我
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (!chart.ClientRectangle.Contains(chart.PointToClient(new Point(e.X,e.Y))))
{
if (ToolTip != null)
ToolTip.Hide(chart);
}
}
我放置跟踪并检查,如果我从图表控件移动到窗体自由空间,事件将触发,仅当从图表移动到其他控件时,Form1\u MouseMove
不会被调用
如何解决我的问题?尝试处理目标控件上的MouseEnter
和MouseLeave
事件(您案例中的图表,我的按钮)
您是否也尝试了图表.PointToClient(Cursor.Position)
?@Sebastian-如果我使用Cursor.Position,我会收到错误CS0176:Member'System.Windows.Forms.Cursor.Current.get'无法通过实例引用访问;用类型名限定它instead@Sebastian,当从图表移动到其他控件时,我放置了跟踪并进行了检查,Form1\u MouseMove本身没有被调用。似乎这是您的问题。如果未调用它,则不会设置事件。在form1.designer.cs中必须是一行this.MouseMove+=new System.Windows.Forms.MouseEventHandler(this.form1\u MouseMove)代码>为什么不使用图表.鼠标移动
事件?因此,您不必关心鼠标指针离开图表后悬停的内容。
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp
{
public partial class Form1 : Form
{
private const string mouseIsOver = "Mouse is over";
private const string mouseIsOutside = "Mouse is outside";
public Form1()
{
InitializeComponent();
var button = new Button { Text = mouseIsOutside, AutoSize = true, Location = new Point(10, 10) };
button.MouseEnter += (sender, e) => button.Text = mouseIsOver;
button.MouseLeave += (sender, e) => button.Text = mouseIsOutside;
this.Controls.Add(button);
}
}
}