Get Mouse State without access to MouseEventArgs?

丶灬走出姿态 提交于 2020-05-28 12:47:28

问题


I have a form with many, many controls. I need to detect if the mouse is down or if it's up. Most of the time, I don't have MouseEventArgs.

Is there a quick and easy way to tell if the mouse is down without mouseEventArgs?

Is there an alternative, or is something like this the only way?:

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;
}

回答1:


You can try checking with a timer:

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



回答2:


ChecK Control.MouseButtons static property:

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


来源:https://stackoverflow.com/questions/10820788/get-mouse-state-without-access-to-mouseeventargs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!