public CroppingAdorner(UIElement adornedElement, Rect rcInit) : base(adornedElement) { if (adornedElement == null) throw new ArgumentNullException("adornedElement"); if (rcInit == null || rcInit == Rect.Empty) { rcInit = new Rect(0, 0, adornedElement.RenderSize.Width, adornedElement.RenderSize.Height); } System.Diagnostics.Debug.WriteLine(String.Format("CroppingAdorner Width={0:N0}, Height={1:N0}", rcInit.Width, rcInit.Height)); _vc = new VisualCollection(this); _prCropMask = new PuncturedRect(); _prCropMask.IsHitTestVisible = false; _prCropMask.RectInterior = rcInit; _prCropMask.Fill = Fill; _vc.Add(_prCropMask); _cnvThumbs = new Canvas(); _cnvThumbs.HorizontalAlignment = HorizontalAlignment.Stretch; _cnvThumbs.VerticalAlignment = VerticalAlignment.Stretch; _vc.Add(_cnvThumbs); BuildCorner(ref _crtTop, Cursors.SizeNS); BuildCorner(ref _crtBottom, Cursors.SizeNS); BuildCorner(ref _crtLeft, Cursors.SizeWE); BuildCorner(ref _crtRight, Cursors.SizeWE); BuildCorner(ref _crtTopLeft, Cursors.SizeNWSE); BuildCorner(ref _crtTopRight, Cursors.SizeNESW); BuildCorner(ref _crtBottomLeft, Cursors.SizeNESW); BuildCorner(ref _crtBottomRight, Cursors.SizeNWSE); // Add handlers for Cropping. _crtBottomLeft.DragDelta += new DragDeltaEventHandler(HandleBottomLeft); _crtBottomRight.DragDelta += new DragDeltaEventHandler(HandleBottomRight); _crtTopLeft.DragDelta += new DragDeltaEventHandler(HandleTopLeft); _crtTopRight.DragDelta += new DragDeltaEventHandler(HandleTopRight); _crtTop.DragDelta += new DragDeltaEventHandler(HandleTop); _crtBottom.DragDelta += new DragDeltaEventHandler(HandleBottom); _crtRight.DragDelta += new DragDeltaEventHandler(HandleRight); _crtLeft.DragDelta += new DragDeltaEventHandler(HandleLeft); //add eventhandler to drag and drop adornedElement.MouseLeftButtonDown += new MouseButtonEventHandler(HandleMouseLeftButtonDown); adornedElement.MouseMove += new MouseEventHandler(HandleMouseMove); adornedElement.MouseLeftButtonUp += new MouseButtonEventHandler(HandleMouseLeftButtonUp); // We have to keep the clipping interior withing the bounds of the adorned element // so we have to track it's size to guarantee that... FrameworkElement fel = (adornedElement as FrameworkElement); if (fel != null) { fel.SizeChanged += new SizeChangedEventHandler(AdornedElement_SizeChanged); } }
private static object CoerceRectInterior(DependencyObject d, object value) { PuncturedRect pr = (PuncturedRect)d; Rect rcExterior = pr.RectExterior; Rect rcProposed = (Rect)value; double left = Math.Max(rcProposed.Left, rcExterior.Left); double top = Math.Max(rcProposed.Top, rcExterior.Top); double width = Math.Max(Math.Min(rcProposed.Right, rcExterior.Right) - left, 0); double height = Math.Max(Math.Min(rcProposed.Bottom, rcExterior.Bottom) - top, 0); rcProposed = new Rect(left, top, width, height); return(rcProposed); }
public CroppingAdorner(UIElement adornedElement, Rect rcInit) : base(adornedElement) { this.ClippingRectangle = rcInit; _visuals = new VisualCollection(this); _cropMask = new PuncturedRect(); _cropMask.SetBinding(PuncturedRect.RectInteriorProperty, new Binding("ClippingRectangle") { Source = this, Mode = BindingMode.TwoWay }); _cropMask.IsHitTestVisible = false; _cropMask.Fill = this.Fill; _visuals.Add(_cropMask); _thumbsCanvas = new Canvas(); _thumbsCanvas.HorizontalAlignment = HorizontalAlignment.Stretch; _thumbsCanvas.VerticalAlignment = VerticalAlignment.Stretch; _visuals.Add(_thumbsCanvas); this.BuildCorner(ref _topThumb, Cursors.SizeNS); this.BuildCorner(ref _bottomThumb, Cursors.SizeNS); this.BuildCorner(ref _leftThumb, Cursors.SizeWE); this.BuildCorner(ref _rightThumb, Cursors.SizeWE); this.BuildCorner(ref _topLeftThumb, Cursors.SizeNWSE); this.BuildCorner(ref _topRightThumb, Cursors.SizeNESW); this.BuildCorner(ref _bottomLeftThumb, Cursors.SizeNESW); this.BuildCorner(ref _bottomRightThumb, Cursors.SizeNWSE); this.BuildDragThumb(ref _dragThumb); // Add handlers for Cropping. _bottomLeftThumb.DragDelta += new DragDeltaEventHandler(this.HandleBottomLeft); _bottomRightThumb.DragDelta += new DragDeltaEventHandler(this.HandleBottomRight); _topLeftThumb.DragDelta += new DragDeltaEventHandler(this.HandleTopLeft); _topRightThumb.DragDelta += new DragDeltaEventHandler(this.HandleTopRight); _topThumb.DragDelta += new DragDeltaEventHandler(this.HandleTop); _bottomThumb.DragDelta += new DragDeltaEventHandler(this.HandleBottom); _rightThumb.DragDelta += new DragDeltaEventHandler(this.HandleRight); _leftThumb.DragDelta += new DragDeltaEventHandler(this.HandleLeft); _dragThumb.DragDelta += new DragDeltaEventHandler(this.HandleDrag); // We have to keep the clipping interior withing the bounds of the adorned element // so we have to track it's size to guarantee that... FrameworkElement element = adornedElement as FrameworkElement; if (element != null) { element.SizeChanged += new SizeChangedEventHandler(this.AdornedElement_SizeChanged); } }
public CroppingAdorner(UIElement adornedElement, Rect rcInit) : base(adornedElement) { _vc = new VisualCollection(this); _prCropMask = new PuncturedRect(); _prCropMask.IsHitTestVisible = false; _prCropMask.RectInterior = rcInit; _prCropMask.Fill = Fill; _vc.Add(_prCropMask); _cnvThumbs = new Canvas(); _cnvThumbs.HorizontalAlignment = HorizontalAlignment.Stretch; _cnvThumbs.VerticalAlignment = VerticalAlignment.Stretch; _vc.Add(_cnvThumbs); BuildCorner(ref _crtTop, Cursors.SizeNS); BuildCorner(ref _crtBottom, Cursors.SizeNS); BuildCorner(ref _crtLeft, Cursors.SizeWE); BuildCorner(ref _crtRight, Cursors.SizeWE); BuildCorner(ref _crtTopLeft, Cursors.SizeNWSE); BuildCorner(ref _crtTopRight, Cursors.SizeNESW); BuildCorner(ref _crtBottomLeft, Cursors.SizeNESW); BuildCorner(ref _crtBottomRight, Cursors.SizeNWSE); // Add handlers for Cropping. _crtBottomLeft.DragDelta += new DragDeltaEventHandler(HandleBottomLeft); _crtBottomRight.DragDelta += new DragDeltaEventHandler(HandleBottomRight); _crtTopLeft.DragDelta += new DragDeltaEventHandler(HandleTopLeft); _crtTopRight.DragDelta += new DragDeltaEventHandler(HandleTopRight); _crtTop.DragDelta += new DragDeltaEventHandler(HandleTop); _crtBottom.DragDelta += new DragDeltaEventHandler(HandleBottom); _crtRight.DragDelta += new DragDeltaEventHandler(HandleRight); _crtLeft.DragDelta += new DragDeltaEventHandler(HandleLeft); // We have to keep the clipping interior withing the bounds of the adorned element // so we have to track it's size to guarantee that... FrameworkElement fel = adornedElement as FrameworkElement; if (fel != null) { fel.SizeChanged += new SizeChangedEventHandler(AdornedElement_SizeChanged); } }