.NET:如何检查鼠标是否在控制? [英] .NET: How to check if the mouse is in a control?

查看:96
本文介绍了.NET:如何检查鼠标是否在控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如果鼠标在.NET中一个特定的控制

i want to know if the mouse is in a particular control in .NET

private void panel1_MouseLeave(object sender, EventArgs e)
{
   if (MouseIsInControl((Control)sender)
      return; //the mouse didn't leave, don't fire a MouseLeave event

   ...
}

public Boolean MouseIsInControl(Control control)
{
    //return (control.Bounds.Contains(MousePosition));
    return control.Bounds.Contains(control.PointToClient(MousePosition))
}

但我需要有人来摆弄了四种不同的坐标系,使其工作<一个href="http://stackoverflow.com/questions/347439/custom-controls-in-c-sharp-windows-forms-mouse-event-question">.

  • <一个href="http://stackoverflow.com/questions/986529/how-to-detect-if-the-mouse-is-inside-the-whole-form-and-child-controls-in-c">How检测鼠标是否在C#中的整个窗体和子控件内?(整个表格)
  • <一个href="http://stackoverflow.com/questions/347439/custom-controls-in-c-sharp-windows-forms-mouse-event-question">Custom在C#中的Windows窗体控件的鼠标事件问题(控制标签内)
  • How to detect if the mouse is inside the whole form and child controls in C#? (entire form)
  • Custom controls in C# Windows Forms mouse event question (control with label inside)

推荐答案

这<一href="http://stackoverflow.com/questions/423728/winform-determine-if-mouse-has-left-user-control/425361#425361">Hans顺便回答可适应做你想要的:

private bool mEntered;
private Timer timer1;

public Form1() {
  InitializeComponent();

  timer1 = new Timer();
  timer1.Interval = 200;
  timer1.Tick += timer1_Tick;
  timer1.Enabled = false;
}

private void panel1_MouseEnter(object sender, EventArgs e) {
  timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e) {
  bool entered = panel1.ClientRectangle.Contains(panel1.PointToClient(Cursor.Position));
  if (entered != mEntered) {
    mEntered = entered;
    if (!entered) {
      timer1.Enabled = false;
      // OK, Do something, the mouse left the parent container
    }
  }
}

这篇关于.NET:如何检查鼠标是否在控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆