Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event handler to detect mouse drag on picture box(winforms,c#)

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.

like image 921
Win Coder Avatar asked Dec 17 '12 15:12

Win Coder


1 Answers

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;
}
like image 112
Mir Avatar answered Oct 22 '22 08:10

Mir