private void ExecuteHandleDrag(ResizingThumb hitThumb, double horizontalChange, double verticalChange) { double currentScale = Scale; double offsetX = hitThumb.IsLeft ? Math.Min(Canvas.GetLeft(adornedElement), -horizontalChange) : horizontalChange; double offsetY = hitThumb.IsTop ? Math.Min(Canvas.GetTop(adornedElement), -verticalChange) : verticalChange; double scaleX = (adornedElement.ActualWidth * Scale + offsetX) / adornedElement.ActualWidth; double scaleY = (adornedElement.ActualHeight * Scale + offsetY) / adornedElement.ActualHeight; Scale = Math.Min(scaleX, scaleY); Scale = Math.Max(0.5, Scale); if (hitThumb.IsLeft) { double move = (Scale - currentScale) * adornedElement.ActualWidth; Canvas.SetLeft(adornedElement, Canvas.GetLeft(adornedElement) - move); } if (hitThumb.IsTop) { double move = (Scale - currentScale) * adornedElement.ActualHeight; Canvas.SetTop(adornedElement, Canvas.GetTop(adornedElement) - move); } adornedElement.LayoutTransform = new ScaleTransform(Scale, Scale); OnWidgetDragging(new Point( Canvas.GetLeft(adornedElement) + adornedElement.ActualWidth * Scale, Canvas.GetTop(adornedElement) + adornedElement.ActualHeight * Scale)); }
/// <summary> /// Initialize the ResizingAdorner /// </summary> /// <param name="adornedElement"></param> public ResizingAdorner(FrameworkElement adornedElement) : base(adornedElement) { visualChildren = new VisualCollection(this); // Call a helper method to initialize the Thumbs // with a customized cursors. topLeft = BuildAdornerCorner(true, true, Cursors.SizeNWSE); topRight = BuildAdornerCorner(true, false, Cursors.SizeNESW); bottomLeft = BuildAdornerCorner(false, true, Cursors.SizeNESW); bottomRight = BuildAdornerCorner(false, false, Cursors.SizeNWSE); }
protected override void HandleDrag(ResizingThumb hitThumb, DragDeltaEventArgs args) { ExecuteHandleDrag(hitThumb, args.HorizontalChange,args.VerticalChange); }
private void HandleDragMethod(ResizingThumb hitThumb, double argsHorizontalChange, double argsVerticalChange) { // Ensure that the Width and Height are properly initialized after the resize. EnforceSize(); // Change the size by the amount the user drags the mouse, as long as it's larger // than the width or height of an adorner, respectively. if (hitThumb.IsLeft) { double widthCanChange = adornedElement.Width - hitThumb.DesiredSize.Width; double horizontalChange = Math.Min(argsHorizontalChange, widthCanChange); Canvas.SetLeft(adornedElement, Canvas.GetLeft(adornedElement) + horizontalChange); adornedElement.Width -= horizontalChange; } else { adornedElement.Width = Math.Max(adornedElement.Width + argsHorizontalChange, hitThumb.DesiredSize.Width); } if (hitThumb.IsTop) { double heightCanChange = adornedElement.Height - hitThumb.DesiredSize.Height; double verticalChange = Math.Min(argsVerticalChange, heightCanChange); Canvas.SetTop(adornedElement, Canvas.GetTop(adornedElement) + verticalChange); adornedElement.Height -= verticalChange; } else { adornedElement.Height = Math.Max(adornedElement.Height + argsVerticalChange, hitThumb.DesiredSize.Height); } OnWidgetDragging(new Point( Canvas.GetLeft(adornedElement) + adornedElement.Width, Canvas.GetTop(adornedElement) + adornedElement.Height)); }
// Helper method to instantiate the corner Thumbs, set the Cursor property, // set some appearance properties, and add the elements to the visual tree. private ResizingThumb BuildAdornerCorner(bool isTop, bool isLeft, Cursor customizedCursor) { ResizingThumb cornerThumb = new ResizingThumb(isTop, isLeft); cornerThumb.Cursor = customizedCursor; cornerThumb.BorderBrush = Brushes.Black; cornerThumb.Background = Brushes.White; cornerThumb.BorderThickness = new Thickness(1); cornerThumb.Height = cornerThumb.Width = 5; cornerThumb.DragDelta += HandleDrag; visualChildren.Add(cornerThumb); return cornerThumb; }
protected virtual void HandleDrag(ResizingThumb hitThumb, DragDeltaEventArgs args) { HandleDragMethod(hitThumb, args.HorizontalChange,args.VerticalChange); }