无法访问MouseEventArgs获取鼠标状态?

时间:2012-05-30 16:55:26

标签: c# .net winforms mouse

我有一个包含许多控件的表单。我需要检测鼠标是否已关闭或是否已启动。大多数时候,我没有 MouseEventArgs

在没有mouseEventArgs的情况下,是否可以快速简便地判断鼠标是否已关闭?

有替代方案,还是这样的唯一途径?:

foreach (Control c in this.Controls)
{
    c.MouseUp += new MouseEventHandler(globalMouseUp);
    c.MouseDown += new MouseEventHandler(globalMouseDown);
}


bool isMouseUp = true;


private void globalMouseDown(object sender, MouseEventArgs e)
{
    isMouseUp = false;
}

private void globalMouseUp(object sender, MouseEventArgs e)
{
    isMouseUp = true;
}

2 个答案:

答案 0 :(得分:16)

您可以尝试使用计时器进行检查:

private void timer1_Tick(object sender, EventArgs e) {
  this.Text = "Mouse Is " + (Control.MouseButtons == MouseButtons.Left);
}

答案 1 :(得分:3)

ChecK Control.MouseButtons静态属性:

if (Control.MouseButtons == MouseButtons.Left)
{
}