我有一个包含许多控件的表单。我需要检测鼠标是否已关闭或是否已启动。大多数时候,我没有 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;
}
答案 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)
{
}