See if left mouse button is held down in the OnMou

2019-06-19 05:26发布

问题:

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