void AssociatedObject_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { Point point = e.GetPosition(rect); hitPoint = RectangleHelper.HitTest(point, rect); if (canvas == null) { canvas = (Canvas)VisualTreeHelper.GetParent(this.AssociatedObject); } FrameworkElement element = AssociatedObject as FrameworkElement; if (hitPoint != HitPoint.None) { height = rect.ActualHeight; width = rect.ActualWidth; rect.Stroke = new SolidColorBrush(Color); if (rect.StrokeThickness <= 0 && Thickness > 0) { rect.StrokeThickness = Thickness; } isDragging = true; mouseOffset = e.GetPosition(AssociatedObject); mouseOffset2 = e.GetPosition(canvas); AssociatedObject.CaptureMouse(); } else { rect.Stroke = borderBrush; rect.StrokeThickness = borderThickness; } }
void AssociatedObject_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { if (e.ClickCount > 1) { return; } if (canvas == null) { canvas = (Canvas)VisualTreeHelper.GetParent(this.AssociatedObject); } rect = GetRectangle(AssociatedObject); Point point = e.GetPosition(rect); HitPoint hitPoint = RectangleHelper.HitTest(point, rect); if (hitPoint == HitPoint.Center || hitPoint == HitPoint.None) { isDragging = true; mouseOffset = e.GetPosition(AssociatedObject); AssociatedObject.CaptureMouse(); } }
void AssociatedObject_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) { Rectangle rect = GetRectangle(AssociatedObject); FrameworkElement element = AssociatedObject as FrameworkElement; if (element.Width < 10 || element.Height < 10) { return; } Point point = e.GetPosition(element); if (isDragging) { isChanged = false; double newWidth = width; double newHeight = height; if (hitPoint == HitPoint.Right) { newWidth = point.X + 1; } else if (hitPoint == HitPoint.Bottom) { newHeight = point.Y + 1; } else { Canvas canvas = (Canvas)VisualTreeHelper.GetParent(AssociatedObject); Point point2 = e.GetPosition(canvas); if (hitPoint == HitPoint.Top) { AssociatedObject.SetValue(Canvas.TopProperty, point2.Y - mouseOffset.Y); newHeight = height - (point2.Y - mouseOffset2.Y); isChanged = true; } else if (hitPoint == HitPoint.Left) { AssociatedObject.SetValue(Canvas.LeftProperty, point2.X - mouseOffset.X); newWidth = width - (point2.X - mouseOffset2.X); isChanged = true; } } if (newHeight != height && newHeight > 10) { SetHeight(element, newHeight); isChanged = true; } if (newWidth != width && newWidth > 10) { SetWidth(element, newWidth); isChanged = true; } if (isChanged) { OnSizeChanged(element, false); } } else { hitPoint = RectangleHelper.HitTest(point, rect); if (hitPoint != HitPoint.None) { rect.Stroke = new SolidColorBrush(Color); if (rect.StrokeThickness <= 0 && Thickness > 0) { rect.StrokeThickness = Thickness; } } else { rect.Stroke = borderBrush; rect.StrokeThickness = borderThickness; } } RectangleHelper.SetCursor(hitPoint); }