protected override void OnPreviewMouseMove(System.Windows.Input.MouseEventArgs e) { base.OnPreviewMouseMove(e); if (!IsEnabled) { return; } Rect newBounds = this.initialBounds; Point pos = e.GetPosition(this); double dx = pos.X - mouseDownPosition.X; double dy = pos.Y - mouseDownPosition.Y; if (dragging == Corner.Middle) { newBounds.X += dx; newBounds.Y += dy; this.bounds = newBounds; PinToLimits(); InvalidateVisual(); } else if (dragging != Corner.None) { // actually move the shape! switch (dragging) { case Corner.TopLeft: newBounds.X += dx; newBounds.Y += dy; newBounds.Width = Math.Max(0, newBounds.Width - dx); newBounds.Height = Math.Max(0, newBounds.Height - dy); break; case Corner.TopMiddle: newBounds.Y += dy; newBounds.Height = Math.Max(0, newBounds.Height - dy); break; case Corner.TopRight: newBounds.Width = Math.Max(0, newBounds.Width + dx); newBounds.Height = Math.Max(0, newBounds.Height - dy); newBounds.Y += dy; break; case Corner.MiddleLeft: newBounds.Width = Math.Max(0, newBounds.Width - dx); newBounds.X += dx; break; case Corner.MiddleRight: newBounds.Width = Math.Max(0, newBounds.Width + dx); break; case Corner.BottomLeft: newBounds.X += dx; newBounds.Height = Math.Max(0, newBounds.Height + dy); newBounds.Width = Math.Max(0, newBounds.Width - dx); break; case Corner.BottomMiddle: newBounds.Height = Math.Max(0, newBounds.Height + dy); break; case Corner.BottomRight: newBounds.Width = Math.Max(0, newBounds.Width + dx); newBounds.Height = Math.Max(0, newBounds.Height + dy); break; } this.bounds = newBounds; PinToLimits(); InvalidateVisual(); if (Resizing != null) { Resizing(this, EventArgs.Empty); } } else { if (TopLeftThumb.Contains(pos)) { Cursor = System.Windows.Input.Cursors.SizeNWSE; } else if (TopMiddleThumb.Contains(pos)) { Cursor = System.Windows.Input.Cursors.SizeNS; } else if (TopRightThumb.Contains(pos)) { Cursor = System.Windows.Input.Cursors.SizeNESW; } else if (MiddleLeftThumb.Contains(pos)) { Cursor = System.Windows.Input.Cursors.SizeWE; } else if (MiddleRightThumb.Contains(pos)) { Cursor = System.Windows.Input.Cursors.SizeWE; } else if (BottomLeftThumb.Contains(pos)) { Cursor = System.Windows.Input.Cursors.SizeNESW; } else if (BottomMiddleThumb.Contains(pos)) { Cursor = System.Windows.Input.Cursors.SizeNS; } else if (BottomRightThumb.Contains(pos)) { Cursor = System.Windows.Input.Cursors.SizeNWSE; } else { Cursor = System.Windows.Input.Cursors.Arrow; } } }
protected override void OnPreviewMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e) { base.OnPreviewMouseLeftButtonDown(e); if (!IsEnabled) { // resizing is not enabled. dragging = Corner.None; return; } Cursor = System.Windows.Input.Cursors.Arrow; Point pos = e.GetPosition(this); if (TopLeftThumb.Contains(pos)) { dragging = Corner.TopLeft; } else if (TopMiddleThumb.Contains(pos)) { dragging = Corner.TopMiddle; } else if (TopRightThumb.Contains(pos)) { dragging = Corner.TopRight; } else if (MiddleLeftThumb.Contains(pos)) { dragging = Corner.MiddleLeft; } else if (MiddleRightThumb.Contains(pos)) { dragging = Corner.MiddleRight; } else if (BottomLeftThumb.Contains(pos)) { dragging = Corner.BottomLeft; } else if (BottomMiddleThumb.Contains(pos)) { dragging = Corner.BottomMiddle; } else if (BottomRightThumb.Contains(pos)) { dragging = Corner.BottomRight; } else { dragging = Corner.Middle; } initialBounds = bounds; e.Handled = true; mouseDownPosition = pos; System.Windows.Input.Mouse.Capture(this); if (timer == null) { timer = new DispatcherTimer(TimeSpan.FromMilliseconds(30), DispatcherPriority.Normal, OnTimerTick, this.Dispatcher); timer.Start(); } }