WinForm控件拖拽类
前言:卖麻辣烫归来后实在太累,写代码来消遣,最近搞Winform,感觉标题栏太丑,就想拖拽窗体,自己写了个拖拽控件类,方便自己使用,没啥技术含量,就是想瞎写写,明天打印出来贴在店里,没事盯着,缅怀我逝去的程序员生涯。
public class DragControl
{
private Control _drag = null;
private Control _move = null;
private Cursor _oldCursor = null;
private int initialX;
private int initialY;
private DragControl() { }
public DragControl(Control drag,Control move)
{
_drag = drag;
_move = move;
}
public DragControl(Control dragControl) : this(dragControl, dragControl) { }
public void CloseDrag()
{
_drag.Cursor = _oldCursor;
_drag.MouseDown -= MoveTipDown;
}
public void OpenDrag()
{
_oldCursor = _drag.Cursor;
_drag.Cursor = Cursors.SizeAll;
_drag.MouseDown += MoveTipDown;
}
private void MoveTipDown(object sender, MouseEventArgs e)
{
initialX = e.X;
initialY = e.Y;
_drag.MouseMove += MoveTipMove;
_drag.MouseUp += MoveTipUp;
}
private void MoveTipUp(object sender, MouseEventArgs e)
{
_drag.MouseMove -= MoveTipMove;
_drag.MouseUp -= MoveTipUp;
}
private void MoveTipMove(object sender, MouseEventArgs e)
{
if (e.Y != initialY) _move.Top = _move.Top + (e.Y - initialY);
if (e.X != initialX) _move.Left = _move.Left + (e.X - initialX);
}
}
{
private Control _drag = null;
private Control _move = null;
private Cursor _oldCursor = null;
private int initialX;
private int initialY;
private DragControl() { }
public DragControl(Control drag,Control move)
{
_drag = drag;
_move = move;
}
public DragControl(Control dragControl) : this(dragControl, dragControl) { }
public void CloseDrag()
{
_drag.Cursor = _oldCursor;
_drag.MouseDown -= MoveTipDown;
}
public void OpenDrag()
{
_oldCursor = _drag.Cursor;
_drag.Cursor = Cursors.SizeAll;
_drag.MouseDown += MoveTipDown;
}
private void MoveTipDown(object sender, MouseEventArgs e)
{
initialX = e.X;
initialY = e.Y;
_drag.MouseMove += MoveTipMove;
_drag.MouseUp += MoveTipUp;
}
private void MoveTipUp(object sender, MouseEventArgs e)
{
_drag.MouseMove -= MoveTipMove;
_drag.MouseUp -= MoveTipUp;
}
private void MoveTipMove(object sender, MouseEventArgs e)
{
if (e.Y != initialY) _move.Top = _move.Top + (e.Y - initialY);
if (e.X != initialX) _move.Left = _move.Left + (e.X - initialX);
}
}