See if left mouse button is held down in the OnMouseMove event

ぐ巨炮叔叔 提交于 2019-12-10 13:27:51

问题


How do I detect if the left mouse button is being held down in the OnMouseMove event for a control?


回答1:


Your eventhandler for the OnMouseMove event should recieve a MouseEventArgs that should tell you if the left button is pressed

private void mouseMoveEventHandler(object sender, MouseEventArgs e)
{
   if(e.Button == MouseButtons.Left)
   {
     //do left stuff
   }
   else 
   {
     // do other stuff
   }
}



回答2:


Simply have a boolean set to true when the left mouse button is held and set it to false when its released.

If you check the condition of the bool when you fire the OnMouseMove event then you will be able to find out if its held down or not.

Psuedo code:

private bool isDown;

MouseDown()
{
   isDown = true;
}

MouseUp()
{
   isDown = false;
}
OnMouseMove()
{
   if(isDown)
   {
       //Do something...
   }
}


来源:https://stackoverflow.com/questions/2186080/see-if-left-mouse-button-is-held-down-in-the-onmousemove-event

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