private void Object_MouseDown(object sender, MouseEventArgs e) { _mouseDownCursorPosition = Cursor.Position; _mouseDownWindowPosition = new Point(Left, Top); _mouseDownWindowSize = Size; _mouseDownBorder = MouseBorder.Unset; (sender as Control).Cursor = Cursors.Default; }
private void Object_MouseMove(object sender, MouseEventArgs e) { var ctrl = sender as Control; bool isBtn = sender is Button; bool isCollectMouseBtn = e.Button == MouseButtons.Left; if (isBtn) { isCollectMouseBtn = e.Button == MouseButtons.Right; } // cursor if (e.Button == MouseButtons.None || _mouseDownBorder == MouseBorder.Unset) { if (isBtn) { _mouseDownBorder = MouseBorder.None; ctrl.Cursor = Cursors.Default; } else { var p = Cursor.Position; bool inX = p.X >= Left && p.X <= Left + Width; bool inY = p.Y >= Top && p.Y <= Top + Height; bool onN = inX && p.Y >= Top - BORDER_SIZE && p.Y <= Top + BORDER_SIZE; bool onS = inX && p.Y >= Top + Height - BORDER_SIZE && p.Y <= Top + Height + BORDER_SIZE; bool onW = inY && p.X >= Left - BORDER_SIZE && p.X <= Left + BORDER_SIZE; bool onE = inY && p.X >= Left + Width - BORDER_SIZE && p.X <= Left + Width + BORDER_SIZE; if (onN) { _mouseDownBorder = MouseBorder.N; ctrl.Cursor = Cursors.SizeNS; } else if (onS) { _mouseDownBorder = MouseBorder.S; ctrl.Cursor = Cursors.SizeNS; } else if (onW) { _mouseDownBorder = MouseBorder.W; ctrl.Cursor = Cursors.SizeWE; } else if (onE) { _mouseDownBorder = MouseBorder.E; ctrl.Cursor = Cursors.SizeWE; } else { _mouseDownBorder = MouseBorder.None; ctrl.Cursor = Cursors.Default; } } } // move & resize if (isCollectMouseBtn) { int newTop = _mouseDownWindowPosition.Y + Cursor.Position.Y - _mouseDownCursorPosition.Y; int newLeft = _mouseDownWindowPosition.X + Cursor.Position.X - _mouseDownCursorPosition.X; int newHeightN = _mouseDownWindowSize.Height + _mouseDownCursorPosition.Y - Cursor.Position.Y; int newHeightS = _mouseDownWindowSize.Height - _mouseDownCursorPosition.Y + Cursor.Position.Y; int newWidthW = _mouseDownWindowSize.Width + _mouseDownCursorPosition.X - Cursor.Position.X; int newWidthE = _mouseDownWindowSize.Width - _mouseDownCursorPosition.X + Cursor.Position.X; switch (_mouseDownBorder) { case MouseBorder.None: Top = newTop; Left = newLeft; ctrl.Cursor = Cursors.SizeAll; break; case MouseBorder.N: int minHeight = MinimumSize.Height, maxHeight = MaximumSize.Height; if ((minHeight == 0 || newHeightN > minHeight) && (maxHeight == 0 || newHeightN < maxHeight)) { Top = newTop; Height = newHeightN; } break; case MouseBorder.S: Height = newHeightS; break; case MouseBorder.W: int minWidth = MinimumSize.Width, maxWidth = MaximumSize.Width; if ((minWidth == 0 || newWidthW > minWidth) && (maxWidth == 0 || newWidthW < maxWidth)) { Left = newLeft; Width = newWidthW; } break; case MouseBorder.E: Width = newWidthE; break; } } }
private void Object_MouseUp(object sender, MouseEventArgs e) { _mouseDownBorder = MouseBorder.Unset; (sender as Control).Cursor = Cursors.Default; }