See if left mouse button is held down in the OnMou

2019-06-19 05:28发布

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

2条回答
爷的心禁止访问
2楼-- · 2019-06-19 05:40

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
   }
}
查看更多
闹够了就滚
3楼-- · 2019-06-19 05:55

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...
   }
}
查看更多
登录 后发表回答