{
Form _form=null ;
int _y = 0, _x = 0;
private bool _mousedown = false;
public FormDrag(Form form)
{
if (form == null)
throw new ArgumentNullException("Must pass a form instance.");
_form = form;
_form.MouseMove += new MouseEventHandler(_form_MouseMove);
_form.MouseDown += new MouseEventHandler(_form_MouseDown);
_form.MouseUp += new MouseEventHandler(_form_MouseUp);
}
void _form_MouseUp(object sender, MouseEventArgs e)
{
_mousedown = false;
}
void _form_MouseDown(object sender, MouseEventArgs e)
{
_mousedown = true;
_y = e.Y;
_x = e.X;
}
void _form_MouseMove(object sender, MouseEventArgs e)
{
if (_mousedown)
{
int a = _y - e.Y;
_form.Top -= a;
a = _x - e.X;
_form.Left -= a;
}
}