15

我有一个包含许多控件的表单。我需要检测鼠标是向下还是向上。大多数时候,我没有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;
}
4

2 回答 2

25

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

private void timer1_Tick(object sender, EventArgs e) {
  this.Text = "Mouse Is " + (Control.MouseButtons == MouseButtons.Left);
}
于 2012-05-30T16:59:58.000 回答
8

检查 Control.MouseButtons 静态属性:

if (Control.MouseButtons == MouseButtons.Left)
{
}
于 2017-06-22T02:24:40.600 回答