#region
public class ConTrolMoveResize
{
#region 似有成员
bool IsMoving = false;
Point pCtrlLastCoordiante = new Point(0, 0);
Point pCusorOffset = new Point(0, 0);
Point pCursorLastCoordinate = new Point(0, 0);
private Control ctrl = null;
private ScrollableControl Containe = null;
#endregion
#region 私有方法
private void MouseDown(object sender, MouseEventArgs e)
{
if (Containe == null)
{
return;
}
if (e.Button == MouseButtons.Left)
{
IsMoving = true;
pCtrlLastCoordiante.X = ctrl.Left;
pCtrlLastCoordiante.Y = ctrl.Top;
pCursorLastCoordinate.X = Cursor.Position.X;
pCursorLastCoordinate.Y = Cursor.Position.Y;
}
}
private void MouseMove(object sender, MouseEventArgs e)
{
if (Containe == null)
{
return;
}
if (e.Button == MouseButtons.Left)
{
if (this.IsMoving)
{
Point pCursor = new Point(Cursor.Position.X, Cursor.Position.Y);
pCusorOffset.X = pCursor.X - pCursorLastCoordinate.X;
pCusorOffset.Y = pCursor.Y = pCursorLastCoordinate.Y;
ctrl.Left = pCtrlLastCoordiante.X + pCusorOffset.X;
ctrl.Top = pCtrlLastCoordiante.Y + pCusorOffset.Y;
}
}
}
private void MouseUp(object sender, MouseEventArgs e)
{
if (Containe == null)
{
return;
}
if (this.IsMoving)
{
if (pCusorOffset.X == 0 && pCusorOffset.Y == 0)
{
return;
}
if ((pCtrlLastCoordiante.X + pCusorOffset.X + ctrl.Width) > 0)
{
ctrl.Left = pCusorOffset.X + pCtrlLastCoordiante.X;
}
else
{
ctrl.Left = 0;
}
if ((pCtrlLastCoordiante.Y + pCusorOffset.Y + ctrl.Height) > 0)
{
ctrl.Top = pCusorOffset.X + pCtrlLastCoordiante.X;
}
}
}
#endregion
}
#endregion