5

I am making simple paint application in which a line would be drawn whenever someone holds down the mouse button and drags(exactly like in windows paint).

However i am having a hard time finding the suitable event handler for this. MouseDown is simply not working and MouseClick is only jotting down dots whenever i press a mouse down.

Need help in this matter.

Thanks.

1 Answer 1

15

Handle MouseDown and set a boolean variable to true. Handle MouseMove and, if the variable is set to true and the mouse's movement is above your desired treshold, operate. Handle MouseUp and set that variable to false.

Example:

bool _mousePressed;
private void OnMouseDown(object sender, MouseEventArgs e)
{
    _mousePressed = true;
}

private void OnMouseMove(object sender, MouseEventArgs e)
{
    if (_mousePressed)
    {
        //Operate
    }
}

private void OnMouseUp(object sender, MouseEventArgs e)
{
    _mousePressed = false;
}
2
  • 1
    Thank you this helped me to. But why does it leaves spaces when I drag it fast?
    – themhz
    Mar 30, 2015 at 14:57
  • 1
    mouse move doesnt fire for every point over the distance it has traveled, just fires whenever it can. Try drawing a line between your dots.
    – timothy
    Apr 13, 2016 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.