/// <summary> /// This method was brought over from core's Transform.cs class to handle /// the special cases that can be encountered looking for the root visual /// (in Silverlight vs WPF, etc) /// </summary> internal static GeneralTransform GetTransformToRoot(FrameworkElement child) { if (Application.Current != null) { if (child.FlowDirection == FlowDirection.RightToLeft) return child.TransformToVisual(null); return child.TransformToVisual(Application.Current.RootVisual); } else return child.TransformToVisual(null); }
public override bool AreBoundaryIntersecting(FrameworkElement cursorVisual) { RectangleGeometry cursorBounds = new RectangleGeometry(new Rect(0, 0, cursorVisual.ActualWidth, cursorVisual.ActualHeight)); RectangleGeometry targetBounds = new RectangleGeometry(new Rect(0, 0, this.ActualWidth, this.ActualHeight)); cursorBounds.Transform = (Transform)cursorVisual.TransformToVisual(this); return cursorBounds.FillContainsWithDetail(targetBounds) != IntersectionDetail.Empty; }
public override bool AreBoundaryIntersecting(FrameworkElement item) { RectangleGeometry itemBounds = new RectangleGeometry(new Rect(0, 0, item.ActualWidth, item.ActualHeight)); RectangleGeometry rulerBounds = new RectangleGeometry(new Rect(0, 0, this.ActualWidth, this.ActualHeight)); itemBounds.Transform = (Transform)item.TransformToVisual(this); return itemBounds.FillContainsWithDetail(rulerBounds) != IntersectionDetail.Empty; }
private static bool IsInView(FrameworkElement element, FrameworkElement container) { if (element == null || element.Visibility == Visibility.Collapsed) { return false; } var bounds = element.TransformToVisual(container).TransformBounds(new Rect(0, 0, element.ActualWidth, element.ActualHeight)); var rect = new Rect(0.0, 0.0, container.ActualWidth, container.ActualHeight); return rect.Contains(new Point(bounds.X, bounds.Y)) || rect.Contains(new Point(bounds.X, bounds.Y + bounds.Height)); }
public System.Windows.Point[] getCorners(FrameworkElement parent, FrameworkElement child) { var t = child.TransformToVisual(parent); System.Windows.Point topLeft = t.Transform(new System.Windows.Point(0, 0)); System.Windows.Point topRight = t.Transform(new System.Windows.Point(child.ActualWidth, 0)); System.Windows.Point bottomLeft = t.Transform(new System.Windows.Point(0, child.ActualHeight)); System.Windows.Point bottomRight = t.Transform(new System.Windows.Point(child.ActualWidth, child.ActualHeight)); return new System.Windows.Point[] { topLeft, topRight, bottomLeft, bottomRight }; }
internal Rect AbsoluteRect(FrameworkElement element) { GeneralTransform generalTransform = #if WPF element.TransformToAncestor(this); #endif #if SILVERLIGHT element.TransformToVisual(this); #endif Point location = generalTransform.Transform(new Point(0, 0)); return(new Rect(location.X, location.Y, element.ActualWidth, element.ActualHeight)); }
public static Point GetRelativePosition(FrameworkElement e, FrameworkElement relativeTo, Corner p) { GeneralTransform gt = e.TransformToVisual(relativeTo); Point po = new Point(); if (p == Corner.LeftTop) po = gt.Transform(new Point(0, 0)); if (p == Corner.LeftBottom) po = gt.Transform(new Point(0, e.ActualHeight)); if (p == Corner.RightTop) po = gt.Transform(new Point(e.ActualWidth, 0)); if (p == Corner.RightBottom) po = gt.Transform(new Point(e.ActualWidth, e.ActualHeight)); return po; }
internal static GeneralTransform GetGeneralTransform(FrameworkElement element) { GeneralTransform transform = null; #if WPF Visual parent = VisualTreeHelper.GetParent(element) as Visual; if (parent != null) { transform = element.TransformToVisual(parent); } else { TransformGroup transformGroup = new TransformGroup(); if (element.LayoutTransform != null) { transformGroup.Children.Add(element.LayoutTransform); } if (element.RenderTransform != null) { transformGroup.Children.Add(element.RenderTransform); } transform = transformGroup; } #elif SILVERLIGHT UIElement parent = VisualTreeHelper.GetParent(element) as UIElement; if (parent != null) { transform = element.TransformToVisual(parent); } else { transform = element.RenderTransform; } #endif return transform; }
public static List<UIElement> GetCollisionsAt(FrameworkElement targetelement, Point p, Size pointersize) { p = targetelement.TransformToVisual(Application.Current.RootVisual).Transform(p); Rect r = new Rect(p.X - (pointersize.Width / 2), p.Y - (pointersize.Height / 2), pointersize.Width, pointersize.Height); List<UIElement> collisions = new List<UIElement>(); IEnumerable<UIElement> hits = VisualTreeHelper.FindElementsInHostCoordinates(r, Application.Current.RootVisual); foreach (UIElement uiel in hits) { // COLLISIONE INTERCETTATA ... =)"; if (typeof(Touchable).IsAssignableFrom(uiel.GetType())) { collisions.Add(uiel); } } return collisions; }
public override bool AreBoundaryIntersecting(FrameworkElement cursorVisual) { try { RectangleGeometry cursorBounds = new RectangleGeometry(new Rect(10,10, cursorVisual.ActualWidth-20, cursorVisual.ActualHeight-20)); RectangleGeometry targetBounds = new RectangleGeometry(new Rect((this.ActualWidth / 4), (2 * this.ActualHeight / 3), 1, 1)); cursorBounds.Transform = (Transform)cursorVisual.TransformToVisual(this); return cursorBounds.FillContainsWithDetail(targetBounds) != IntersectionDetail.Empty; } catch (Exception e) { Console.WriteLine("Found exception: " + e); return false; } }
/// <summary /> internal static Point[] GetTranslatedPoints(FrameworkElement element) { Point[] pointArray = new Point[4]; if (element != null) { GeneralTransform transform = element.TransformToVisual(null); pointArray[0] = transform.Transform(new Point(0.0, 0.0)); pointArray[1] = transform.Transform(new Point(element.ActualWidth, 0.0)); pointArray[1].X--; pointArray[2] = transform.Transform(new Point(0.0, element.ActualHeight)); pointArray[2].Y--; pointArray[3] = transform.Transform(new Point(element.ActualWidth, element.ActualHeight)); pointArray[3].X--; pointArray[3].Y--; } return pointArray; }
public int BoundaryIntersectingOnSide(FrameworkElement cursorVisual) { RectangleGeometry cursorBounds = new RectangleGeometry(new Rect(0, 0, cursorVisual.ActualWidth, cursorVisual.ActualHeight)); RectangleGeometry targetBoundsTop = new RectangleGeometry(new Rect(0, 0, this.ActualWidth, 1)); RectangleGeometry targetBoundsBottom = new RectangleGeometry(new Rect(0, this.ActualHeight-1, this.ActualWidth, 1)); RectangleGeometry targetBoundsLeft = new RectangleGeometry(new Rect(0, 5, 1, this.ActualHeight-10)); RectangleGeometry targetBoundsRight = new RectangleGeometry(new Rect(this.ActualWidth - 1, 5, 1, this.ActualHeight-10)); cursorBounds.Transform = (Transform)cursorVisual.TransformToVisual(this); if (cursorBounds.FillContainsWithDetail(targetBoundsLeft) != IntersectionDetail.Empty) { Console.WriteLine("FOUND A LEFT"); return LEFT; } else if (cursorBounds.FillContainsWithDetail(targetBoundsRight) != IntersectionDetail.Empty) { Console.WriteLine("FOUND A RIGHT"); return RIGHT; } else if(cursorBounds.FillContainsWithDetail(targetBoundsTop) != IntersectionDetail.Empty) { Console.WriteLine("FOUND A TOP"); return TOP; } else //if (cursorBounds.FillContainsWithDetail(targetBoundsBottom) != IntersectionDetail.Empty) { Console.WriteLine("FOUND A BOTTOM"); return BOTTOM; } }
public RenderTargetBitmap SaveControl(FrameworkElement Target) { FrameworkElement Parent = Target.Parent as FrameworkElement; double ParentWidth, ParentHeight; if (Parent == null) { ParentWidth = 1000; ParentHeight = 1000; Canvas ParentCanvas = new Canvas(); ParentCanvas.Children.Add(Target); Parent = ParentCanvas; } else { ParentWidth = Parent.ActualWidth; ParentHeight = Parent.ActualHeight; } Target.Measure(new Size(ParentWidth, ParentHeight)); Target.Arrange(new Rect(0, 0, ParentWidth, ParentHeight)); Target.Measure(new Size(Target.ActualWidth, Target.ActualHeight)); Target.Arrange(new Rect(0, 0, Target.ActualWidth, Target.ActualHeight)); Rect Rect = Target.TransformToVisual(Parent).TransformBounds(new Rect(0, 0, Target.ActualWidth, Target.ActualHeight)); Target.Arrange(new Rect(-Rect.Left, -Rect.Top, Target.ActualWidth, Target.ActualHeight)); RenderTargetBitmap r = new RenderTargetBitmap((int)Rect.Width, (int)Rect.Height, 96.0, 96.0, PixelFormats.Pbgra32); r.Render(Target); return r; }
/// <summary> /// Indicates whether the specified framework element /// is within the bounds of the application's root visual. /// </summary> /// <param name="element">The framework element.</param> /// <returns> /// True if the rectangular bounds of the framework element /// are completely outside the bounds of the application's root visual. /// </returns> private static bool IsOnScreen(FrameworkElement element) { PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame; if (root == null) { return false; } GeneralTransform generalTransform; double height = root.ActualHeight; try { generalTransform = element.TransformToVisual(root); } catch (ArgumentException) { return false; } Rect bounds = new Rect( generalTransform.Transform(new Point(0, 0)), generalTransform.Transform(new Point(element.ActualWidth, element.ActualHeight))); return (bounds.Bottom > 0 && bounds.Top < height); }
internal void ScrollIntoView(FrameworkElement element) { // Get the ScrollHost ScrollViewer scrollHost = this.ScrollHost; if (scrollHost == null) { return; } // Get the position of the element relative to the ScrollHost GeneralTransform transform = null; try { transform = element.TransformToVisual(scrollHost); } catch (ArgumentException) { // Ignore failures when not in the visual tree return; } Rect itemRect = new Rect( transform.Transform(new Point()), transform.Transform(new Point(element.ActualWidth, element.ActualHeight))); // Scroll vertically double verticalOffset = scrollHost.VerticalOffset; double verticalDelta = 0; double hostBottom = scrollHost.ViewportHeight; double itemBottom = itemRect.Bottom; if (hostBottom < itemBottom) { verticalDelta = itemBottom - hostBottom; verticalOffset += verticalDelta; } double itemTop = itemRect.Top; if (itemTop - verticalDelta < 0) { verticalOffset -= verticalDelta - itemTop; } scrollHost.ScrollToVerticalOffset(verticalOffset); // Scroll horizontally double horizontalOffset = scrollHost.HorizontalOffset; double horizontalDelta = 0; double hostRight = scrollHost.ViewportWidth; double itemRight = itemRect.Right; if (hostRight < itemRight) { horizontalDelta = itemRight - hostRight; horizontalOffset += horizontalDelta; } double itemLeft = itemRect.Left; if (itemLeft - horizontalDelta < 0) { horizontalOffset -= horizontalDelta - itemLeft; } scrollHost.ScrollToHorizontalOffset(horizontalOffset); }
/// <summary> /// Snaps the GraphPoint to the nearest Pixel. /// </summary> /// <param name="element">Framework to be Snapped to Grid.</param> internal void SnapGraphPoint(FrameworkElement element) { if (element == null || this.DynamicPlotLayer == null) { return; } bool snap = GraphBase.GetSnapToPixels(element); if (snap == false) { return; } // Remove existing transform TranslateTransform savedTransform = element.RenderTransform as TranslateTransform; if (savedTransform != null) { element.RenderTransform = null; } // Calculate actual location Point zero = new Point(0, 0); MatrixTransform globalTransform = element.TransformToVisual(Application.Current.RootVisual) as MatrixTransform; Point p = globalTransform.Matrix.Transform(zero); double deltaX = (int)p.X - p.X; double deltaY = (int)p.Y - p.Y; // Set new transform if (deltaX != 0 || deltaY != 0) { if (savedTransform == null) { savedTransform = new TranslateTransform(); } element.RenderTransform = savedTransform; savedTransform.X = deltaX; savedTransform.Y = deltaY; } }
/// <summary> /// Scrolls the and focus. /// </summary> /// <param name="element">The element.</param> private static void ScrollAndFocus(FrameworkElement element) { var scrollViewer = element.GetParentByName("FieldsScrollView") as ScrollViewer; if (scrollViewer == null) return; try { var focusedVisualTransform = element.TransformToVisual(scrollViewer); var rectangle = focusedVisualTransform.TransformBounds(new Rect(new Point(element.Margin.Left, element.Margin.Top), element.RenderSize)); var newOffset = scrollViewer.VerticalOffset + rectangle.Top - 80; scrollViewer.ScrollToVerticalOffset(newOffset); var control = element as Control; if (control != null && control.IsVisibile()) { control.Focus(); } } catch (Exception) { } }
FrameworkElement _elementhook(FrameworkElement source, TouchGesture gesturedirection) { if (source == null) return null; Point cornertest = new Point(); double hookdistance = 100; cornertest.X = source.ActualWidth / 2; cornertest.Y = source.ActualHeight / 2; if (gesturedirection == TouchGesture.MOVE_NORTH || gesturedirection == TouchGesture.MOVE_NORTHEAST || gesturedirection == TouchGesture.MOVE_NORTHWEST) { cornertest.Y = source.ActualHeight + hookdistance; } if (gesturedirection == TouchGesture.MOVE_SOUTH || gesturedirection == TouchGesture.MOVE_SOUTHEAST || gesturedirection == TouchGesture.MOVE_SOUTHWEST) { cornertest.Y = -hookdistance; } if (gesturedirection == TouchGesture.MOVE_EAST || gesturedirection == TouchGesture.MOVE_NORTHEAST || gesturedirection == TouchGesture.MOVE_SOUTHEAST) { cornertest.X = -hookdistance; } if (gesturedirection == TouchGesture.MOVE_WEST || gesturedirection == TouchGesture.MOVE_NORTHWEST || gesturedirection == TouchGesture.MOVE_SOUTHWEST) { cornertest.X = source.ActualWidth + hookdistance; } this.Clip = null; FrameworkElement hookedelement = null; Point ht = source.TransformToVisual(Application.Current.RootVisual).Transform(cornertest); Rect rht = new Rect() { X = ht.X - 10, Y = ht.Y - 10, Width = 20, Height = 20 }; IEnumerable<UIElement> hitsn = VisualTreeHelper.FindElementsInHostCoordinates(rht, Application.Current.RootVisual); foreach (FrameworkElement el in hitsn) { bool hookable = (bool)el.GetValue(HookableProperty); if (el.Parent != null /* && !el.Equals(_scrollercanvas) */ && hookable) { hookedelement = el; break; } } // _clipview(); // if (hookedelement != null) { ZoomToElement(hookedelement); } return hookedelement; }
Rect getBounds(FrameworkElement of, FrameworkElement from) { GeneralTransform transform = of.TransformToVisual(from); Rect r = transform.TransformBounds(new Rect(0, 0, of.ActualWidth, of.ActualHeight)); return r; }
/// <summary> /// Computes the delta between the centre of an element and its container /// </summary> /// <param name="element">The element to compare</param> /// <param name="container">The element to compare against</param> /// <returns>A point that represents the delta between the two centers</returns> static Point GetCenterToCenterDelta(FrameworkElement element, FrameworkElement container) { Point elementCenter = new Point(element.ActualWidth / 2, element.ActualHeight / 2); Point containerCenter; #if WINDOWS_PHONE // need to special-case the frame because it lies about its width / height if (container is PhoneApplicationFrame) { PhoneApplicationFrame frame = container as PhoneApplicationFrame; // Switch width and height in landscape mode if ((frame.Orientation & PageOrientation.Landscape) == PageOrientation.Landscape) { Debug.WriteLine("Switching container coordinates because it's the root frame"); containerCenter = new Point(container.ActualHeight / 2, container.ActualWidth / 2); } else containerCenter = new Point(container.ActualWidth / 2, container.ActualHeight / 2); } else containerCenter = new Point(container.ActualWidth / 2, container.ActualHeight / 2); #else containerCenter = new Point(container.ActualWidth / 2, container.ActualHeight / 2); #endif Point transformedElementCenter = element.TransformToVisual(container).Transform(elementCenter); Point result = new Point(containerCenter.X - transformedElementCenter.X, containerCenter.Y - transformedElementCenter.Y); Debug.WriteLine("Transforming center " + transformedElementCenter + " to " + containerCenter + "; got " + result); return result; }
private static OverlapKind Overlap(FrameworkElement parent, FrameworkElement child, double padding) { // Get child bounds relative to parent var transform = child.TransformToVisual(parent); var bounds = new Rect(transform.Transform(new Point()), transform.Transform(new Point(/*child.ActualWidth*/ 0, child.ActualHeight))); // Return kind of overlap if (Overlap(0 - padding, parent.ActualHeight + padding, bounds.Top, bounds.Bottom)) { return OverlapKind.Overlap; } else if (bounds.Top < 0) { return OverlapKind.ChildAbove; } else { return OverlapKind.ChildBelow; } }
/// <summary> /// Animate the given element so that it flies off screen, fading /// everything else that is on screen. /// </summary> public void ItemFlyOut(FrameworkElement element, Action action) { _targetElement = element; var rootElement = Application.Current.RootVisual as FrameworkElement; var transform = element.TransformToVisual(rootElement); Point absolutePosition = transform.Transform(new Point(0, 0)); _targetElementPosition = absolutePosition; _backgroundMask = new Rectangle() { Fill = new SolidColorBrush(Colors.Black), Opacity = 0.0, Width = rootElement.ActualWidth, Height = rootElement.ActualHeight }; _popupCanvas.Children.Add(_backgroundMask); _targetElementClone = new Image() { Source = new WriteableBitmap(element, null) }; Canvas.SetTop(_targetElementClone, _targetElementPosition.Y); Canvas.SetLeft(_targetElementClone, 0); _popupCanvas.Children.Add(_targetElementClone); var sb = new Storyboard(); // animate the X position var db = CreateDoubleAnimation(_targetElementPosition.X, _targetElementPosition.X + 500, new SineEase() { EasingMode = EasingMode.EaseIn }, _targetElementClone, Canvas.LeftProperty, _flyOutSpeed); sb.Children.Add(db); // animate the Y position db = CreateDoubleAnimation(_targetElementPosition.Y, _targetElementPosition.Y, new SineEase() { EasingMode = EasingMode.EaseOut }, _targetElementClone, Canvas.TopProperty, _flyOutSpeed); sb.Children.Add(db); // fade out the other elements db = CreateDoubleAnimation(0, 1, null, _backgroundMask, UIElement.OpacityProperty, _flyOutSpeed); sb.Children.Add(db); sb.Completed += (s, e2) => { action(); // hide the popup, by placing a task on the dispatcher queue, this // should be executed after the navigation has occurred element.Dispatcher.BeginInvoke(() => { _popup.IsOpen = false; }); }; // hide the element we have 'cloned' into the popup element.Opacity = 0.0; // open the popup _popup.IsOpen = true; // begin the animation sb.Begin(); }
public static void BringToUp(FrameworkElement ui, FrameworkElement parent) { //获得需要平移的坐标值 MatrixTransform pt = (MatrixTransform)ui.TransformToVisual(parent); CompositeTransform trans = new CompositeTransform(); trans.TranslateX = -pt.Matrix.OffsetX; trans.TranslateY = -pt.Matrix.OffsetY; //计算放大比例 //trans.ScaleX = parent.ActualWidth / ui.ActualWidth; //trans.ScaleY = parent.ActualHeight / ui.ActualHeight; ui.RenderTransform = trans; //放置到最上面 //Canvas.SetZIndex(ui, 100); }
/// <summary> /// Indicates whether the specified framework element /// is within the bounds of the application's root visual. /// </summary> /// <param name="element">The framework element.</param> /// <returns> /// True if the rectangular bounds of the framework element /// are completely outside the bounds of the application's root visual. /// </returns> private static bool IsOnScreen(FrameworkElement element) { PhoneApplicationFrame root = Application.Current.RootVisual as PhoneApplicationFrame; if (root == null) { return false; } GeneralTransform generalTransform; double height = root.ActualHeight; double width = root.ActualWidth; try { generalTransform = element.TransformToVisual(root); } catch (ArgumentException) { return false; } Rect bounds = new Rect( generalTransform.Transform(Origin), generalTransform.Transform(new Point(element.ActualWidth, element.ActualHeight))); bool isParentTransparent = false; IList<FrameworkElement> ancestors = element.GetVisualAncestors().ToList(); if (ancestors != null) { for (int i = 0; i < ancestors.Count; i++) { if (ancestors[i].Opacity <= 0.001) { isParentTransparent = true; break; } } } return (bounds.Bottom > 0) && (bounds.Top < height) && (bounds.Right > 0) && (bounds.Left < width) && !isParentTransparent; }
Point _getfocusshift(FrameworkElement element) { GeneralTransform trel = element.TransformToVisual(_scrollerpanel); Point tt = trel.Transform(new Point()); Point t = tt; t.X = -t.X - _scrollertransformhelper.Translate.X; t.Y = -t.Y - _scrollertransformhelper.Translate.Y; return t; }
/// <summary> /// Causes the element to scroll into view. /// </summary> /// <param name="element"> The element to scroll. </param> /// <exception cref="ArgumentNullException"> The <paramref name="element"/> parameter is null. </exception> public static void ScrollIntoView(FrameworkElement element) { if (element == null) throw new ArgumentNullException("element"); var scrollViewer = element.GetParentByName("FieldsScrollView") as ScrollViewer; if (scrollViewer == null) return; try { var focusedVisualTransform = element.TransformToVisual(scrollViewer); var rectangle = focusedVisualTransform.TransformBounds(new Rect(new Point(element.Margin.Left, element.Margin.Top), element.RenderSize)); var newOffset = scrollViewer.VerticalOffset + rectangle.Top - 80; scrollViewer.ScrollToVerticalOffset(newOffset); } catch (Exception) { } }
internal Point GetLocation (FrameworkElement owner) { if (VisualTreeHelper.GetParent (owner) == null) return new Point (0, 0); Point point = new Point (0, 0); try { // This happens when an item is not visible yet but exists, for // example ListBoxItems in ComboBox when Template is not null point = owner.TransformToVisual (Application.Current.RootVisual).Transform (new Point ()); } catch (ArgumentException) { } return point; }
private Geometry GetClipRegion( RowSelectorDecorator decorator, Rect arrangeRect, FrameworkElement referenceElement ) { if( referenceElement == null ) return null; var rowSelector = decorator.RowSelector; if( rowSelector == null ) return null; GeneralTransform referenceElementToRowSelectorPaneTransform = referenceElement.TransformToVisual( this ); Rect referenceElementRegion = referenceElementToRowSelectorPaneTransform.TransformBounds( new Rect( 0, 0, referenceElement.ActualWidth, referenceElement.ActualHeight ) ); RectangleGeometry clipRegion = null; if( this.Orientation == Orientation.Vertical ) { UIElement container = rowSelector.DataContext as UIElement; if( ( container != null ) && ( container.Clip != null ) ) { Rect containerClipBounds = container.Clip.Bounds; // In this case, we will use the container's clip properties (Top and Bottom). clipRegion = new RectangleGeometry( new Rect( 0d, containerClipBounds.Y, arrangeRect.Width, containerClipBounds.Height ) ); } else if( ( arrangeRect.Top < referenceElementRegion.Top ) || ( arrangeRect.Bottom > referenceElementRegion.Bottom ) ) { double x = 0d; double y = Math.Max( referenceElementRegion.Top - arrangeRect.Top, 0 ); double width = arrangeRect.Width; double height = Math.Max( 0, arrangeRect.Height - y - Math.Max( 0, arrangeRect.Bottom - referenceElementRegion.Bottom ) ); clipRegion = new RectangleGeometry( new Rect( x, y, width, height ) ); } } else { UIElement container = rowSelector.DataContext as UIElement; if( ( container != null ) && ( container.Clip != null ) ) { Rect containerClipBounds = container.Clip.Bounds; // In this case, we will use the container's clip properties (Left and Right). clipRegion = new RectangleGeometry( new Rect( containerClipBounds.X, 0d, containerClipBounds.Width, arrangeRect.Height ) ); } else if( ( arrangeRect.Left < referenceElementRegion.Left ) || ( arrangeRect.Right > referenceElementRegion.Right ) ) { double x = Math.Max( referenceElementRegion.Left - arrangeRect.Left, 0 ); double y = 0d; double width = arrangeRect.Width - x - Math.Max( 0, arrangeRect.Right - referenceElementRegion.Right ); double height = arrangeRect.Height; clipRegion = new RectangleGeometry( new Rect( x, y, width, height ) ); } } return clipRegion; }
/// <summary> /// Prepares a framework element to be feathered by adding a plane projection /// and a composite transform to it. /// </summary> /// <param name="root">The root visual.</param> /// <param name="element">The framework element.</param> private static bool TryAttachProjectionAndTransform(PhoneApplicationFrame root, FrameworkElement element) { GeneralTransform generalTransform; try { generalTransform = element.TransformToVisual(root); } catch (ArgumentException) { return false; } Point coordinates = generalTransform.Transform(Origin); double y = coordinates.Y + element.ActualHeight / 2.0; double offset = (root.ActualHeight / 2.0) - y; // Cache original projection and transform. TurnstileFeatherEffect.SetOriginalProjection(element, element.Projection); TurnstileFeatherEffect.SetOriginalRenderTransform(element, element.RenderTransform); // Attach projection. PlaneProjection projection = new PlaneProjection(); projection.GlobalOffsetY = offset * -1.0; projection.CenterOfRotationX = FeatheringCenterOfRotationX; element.Projection = projection; // Attach transform. Transform originalTransform = element.RenderTransform; TranslateTransform translateTransform = new TranslateTransform(); translateTransform.Y = offset; TransformGroup transformGroup = new TransformGroup(); transformGroup.Children.Add(originalTransform); transformGroup.Children.Add(translateTransform); element.RenderTransform = transformGroup; return true; }
/// <summary> /// 控件截屏 /// </summary> /// <param name="ele">要截屏元素</param> /// <param name="parent">截屏父元素</param> /// <param name="offsetX">截屏X方向偏移量</param> /// <param name="offsetY">截屏Y方向偏移量</param> /// <param name="savePath">保存路径</param> /// <param name="isVisual">是否是Visual</param> public static void ControlClipScreen(FrameworkElement ele, Visual parent, double offsetX, double offsetY, string savePath, bool isVisual) { if (isVisual) //WPF类型控件 { RenderTargetBitmap rtb = new RenderTargetBitmap((int)ele.ActualWidth, (int)ele.ActualHeight, 96d, 96d, PixelFormats.Default); rtb.Render(ele); BmpBitmapEncoder encoder = new BmpBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rtb)); FileStream fs = System.IO.File.Open(savePath, FileMode.Create); encoder.Save(fs); fs.Close(); } else //Winform控件 { Point childPoint = ele.TransformToVisual(parent).Transform(new Point(0, 0)); System.Drawing.Bitmap myImage = new System.Drawing.Bitmap((int)(ele.ActualWidth + offsetX), (int)(ele.ActualHeight + offsetY)); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myImage); g.CopyFromScreen(new System.Drawing.Point((int)childPoint.X, (int)childPoint.Y), new System.Drawing.Point(0, 0), new System.Drawing.Size((int)(ele.ActualWidth - offsetX), (int)(ele.ActualHeight - offsetY))); IntPtr dc1 = g.GetHdc(); g.ReleaseHdc(dc1); g.Dispose(); myImage.Save(savePath); } }
/// <summary> /// Computes the delta between the centre of an element and its /// container. /// </summary> /// <param name="element">The element to compare.</param> /// <param name="container">The element to compare against.</param> /// <returns>A point that represents the delta between the two centers.</returns> static Point GetCenterToCenterDelta(FrameworkElement element, FrameworkElement container) { Point elementCenter = new Point(element.ActualWidth / 2, element.ActualHeight / 2); Point containerCenter; #if WINDOWS_PHONE // Need to special-case the frame to handle different orientations. PhoneApplicationFrame frame = container as PhoneApplicationFrame; if (frame != null) { // Switch width and height in landscape mode if ((frame.Orientation & PageOrientation.Landscape) == PageOrientation.Landscape) { containerCenter = new Point(container.ActualHeight / 2, container.ActualWidth / 2); } else { containerCenter = new Point(container.ActualWidth / 2, container.ActualHeight / 2); } } else { containerCenter = new Point(container.ActualWidth / 2, container.ActualHeight / 2); } #else containerCenter = new Point(container.ActualWidth / 2, container.ActualHeight / 2); #endif Point transformedElementCenter = element.TransformToVisual(container).Transform(elementCenter); Point result = new Point(containerCenter.X - transformedElementCenter.X, containerCenter.Y - transformedElementCenter.Y); return result; }
public static void BreakGroup(SchemaView view) { Canvas parent = (Canvas)view.MainPanel; Viewbox g = view.SelectionManager.SelectedObjects[0] as Viewbox; try { Canvas gc = (Canvas)g.Child; while (gc.Children.Count > 0) { System.Windows.FrameworkElement child = gc.Children[0] as System.Windows.FrameworkElement; //this code need for ungrouping not stretched shapes child.SetValue(System.Windows.Controls.Panel.MarginProperty, System.Windows.DependencyProperty.UnsetValue); //Rect b=VisualTreeHelper.GetContentBounds(child); Rect b = VisualTreeHelper.GetDescendantBounds(child); double left = Canvas.GetLeft(child); double top = Canvas.GetTop(child); left = double.IsNaN(left) ? 0 : left; top = double.IsNaN(top) ? 0 : top; if ((child is Shape) && child.ReadLocalValue(System.Windows.Shapes.Shape.StretchProperty).Equals(System.Windows.DependencyProperty.UnsetValue)) { child.Width = b.Width; child.Height = b.Height; Canvas.SetLeft(child, left + b.X); Canvas.SetTop(child, top + b.Y); child.SetValue(System.Windows.Shapes.Shape.StretchProperty, System.Windows.Media.Stretch.Fill); gc.UpdateLayout(); } // excluding rotate from common matrix // need to separate scale*skew matrix on scale and transform transformations Matrix matrGtr = ((Transform)child.TransformToVisual(parent)).Value; double x = matrGtr.OffsetX; double y = matrGtr.OffsetY; double angleY = Math.Atan(matrGtr.M12 / matrGtr.M11) * 180 / Math.PI; matrGtr.OffsetY = 0; matrGtr.OffsetX = 0; RotateTransform rt = new RotateTransform(angleY); matrGtr.Rotate(-angleY); TransformGroup gtr = new TransformGroup(); gtr.Children.Add(new MatrixTransform(matrGtr)); gtr.Children.Add(rt); child.RenderTransform = gtr; child.RenderTransformOrigin = new Point(0.5, 0.5); Point pO = new Point(child.Width * child.RenderTransformOrigin.X, child.Height * child.RenderTransformOrigin.Y); Point p = gtr.Transform(pO); Canvas.SetLeft(child, x - (pO - p).X); Canvas.SetTop(child, y - (pO - p).Y); gc.Children.Remove(child); parent.Children.Add(child); //tool.NotifyObjectCreated(child); } g.Child = null; //tool.NotifyObjectDeleted(g); parent.Children.Remove(g); view.SelectionManager.SelectObject(null); } catch (Exception) { } }