public static Point PointToWindow(UIElement element, Point pointOnElement) { Window wnd = Window.GetWindow(element); if (wnd == null) { throw new InvalidOperationException("target element is not yet connected to the Window drawing surface"); } return element.TransformToAncestor(wnd).Transform(pointOnElement); }
// Get bounding rectangle, in coords relative to root (not screen) internal static Rect GetLocalRect( UIElement element ) { // Get top-most visual. Visual parent = GetRoot( element ); // Get the points for the rectangle and transform them. double height = element.RenderSize.Height; double width = element.RenderSize.Width; Rect rect = new Rect(0, 0, width, height); GeneralTransform g = element.TransformToAncestor(parent); return g.TransformBounds(rect); }
///<summary> /// This eliminates the part of bounding rectangle if it is at all being overlapped/clipped by any of the visual ancestor up in the parent chain ///</summary> internal static Rect CalculateVisibleBoundingRect(UIElement owner) { Rect boundingRect = new Rect(owner.RenderSize); // Compute visible portion of the rectangle. DependencyObject parent = VisualTreeHelper.GetParent(owner); while (parent != null && !DoubleUtil.AreClose(boundingRect, Rect.Empty) && !DoubleUtil.AreClose(boundingRect.Height, 0) && !DoubleUtil.AreClose(boundingRect.Width, 0)) { Visual visualParent = parent as Visual; if (visualParent != null) { Geometry clipGeometry = VisualTreeHelper.GetClip(visualParent); if (clipGeometry != null) { GeneralTransform transform = owner.TransformToAncestor(visualParent).Inverse; // Safer version of transform to descendent (doing the inverse ourself and saves us changing the co-ordinate space of the owner's bounding rectangle), // we want the rect inside of our space. (Which is always rectangular and much nicer to work with) if (transform != null) { Rect clipBounds = clipGeometry.Bounds; clipBounds = transform.TransformBounds(clipBounds); boundingRect.Intersect(clipBounds); } else { // No visibility if non-invertable transform exists. boundingRect = Rect.Empty; } } } parent = VisualTreeHelper.GetParent(parent); } return boundingRect; }
internal static Point ClientToScreen(UIElement relativeTo, Point point) { GeneralTransform transform; PresentationSource source = PresentationSource.CriticalFromVisual(relativeTo); if (source == null) { return new Point(double.NaN, double.NaN); } transform = relativeTo.TransformToAncestor(source.RootVisual); Point ptRoot; transform.TryTransform(point, out ptRoot); Point ptClient = PointUtil.RootToClient(ptRoot, source); Point ptScreen = PointUtil.ClientToScreen(ptClient, source); return ptScreen; }
protected override Transform GetCurrentLayoutInfo(UIElement element, out Point arrangePosition) { var group = (TransformGroup) element.RenderTransform; Point currentPos = element.TransformToAncestor(this).Transform(new Point()); arrangePosition = new Point(0, 0); if (@group.Inverse != null) arrangePosition = @group.Inverse.Transform(currentPos); return group.Children[1]; }
public static Rect GetControlRectInWindowSpace(UIElement element) { Visual ancestor = (Visual)null; PresentationSource presentationSource = PresentationSource.FromVisual((Visual)element); if (presentationSource != null) ancestor = presentationSource.RootVisual; if (ancestor != null) return element.TransformToAncestor(ancestor).TransformBounds(new Rect(element.RenderSize)); return Rect.Empty; }
public static Point GetControlScaleInWindowSpace(UIElement element) { Visual ancestor = (Visual)null; PresentationSource presentationSource = PresentationSource.FromVisual((Visual)element); if (presentationSource != null) ancestor = presentationSource.RootVisual; if (ancestor == null) return new Point(1.0, 1.0); Transform transform = (Transform)element.TransformToAncestor(ancestor); return new Point(transform.Value.M11, transform.Value.M22); }
internal static GeneralTransform TransformToScreenRoot(UIElement target) { if (BrowserInteropHelper.IsBrowserHosted) { return target.TransformToAncestor(GetRootVisual(target)); } else { var rootWindow = Window.GetWindow(target); if (rootWindow != null) { var transform = target.TransformToAncestor(rootWindow) as Transform; var windowPosition = DpiHelper.DevicePixelsToLogical(rootWindow.PointToScreen(new Point(0, 0))); return new TransformGroup { Children = { transform, new TranslateTransform { X = windowPosition.X, Y = windowPosition.Y, } } }; } else { // This code is ONLY for WindowsForms application that hosts the WPF. var element = GetParents(target).OfType<Visual>().Where(o => o.ReadLocalValue(WindowInteropabilityHelper.WindowInteropabilityAdapterProperty) != DependencyProperty.UnsetValue).FirstOrDefault(); if (element != null) { var adapter = WindowInteropabilityHelper.GetWindowInteropabilityAdapter(element); var transform = target.TransformToAncestor(element) as Transform; return new TransformGroup { Children = { transform, new TranslateTransform { X = adapter.AbsoluteLeft, Y = adapter.AbsoluteTop } } }; } return target.TransformToAncestor(GetRootVisual(target)); } } }