internal static Rect ElementToRoot(Rect rectElement, Visual element, PresentationSource presentationSource) { GeneralTransform transformElementToRoot = element.TransformToAncestor(presentationSource.RootVisual); Rect rectRoot = transformElementToRoot.TransformBounds(rectElement); return rectRoot; }
/// <summary> /// Sets the position of the caret previously created using <see cref="CreateCaret"/>. position is relative to the owner visual. /// </summary> public static bool SetCaretPosition(Visual owner, Point position) { if (owner == null) throw new ArgumentNullException("owner"); HwndSource source = PresentationSource.FromVisual(owner) as HwndSource; if (source != null) { Point pointOnRootVisual = owner.TransformToAncestor(source.RootVisual).Transform(position); Point pointOnHwnd = pointOnRootVisual.TransformToDevice(source.RootVisual); return SafeNativeMethods.SetCaretPos((int)pointOnHwnd.X, (int)pointOnHwnd.Y); } else { return false; } }
public static Point GetMousePosition(Visual relativeTo) { Win32Point mouse = new Win32Point(); GetCursorPos(ref mouse); System.Windows.Interop.HwndSource presentationSource = (System.Windows.Interop.HwndSource)PresentationSource.FromVisual(relativeTo); ScreenToClient(presentationSource.Handle, ref mouse); GeneralTransform transform = relativeTo.TransformToAncestor(presentationSource.RootVisual); Point offset = transform.Transform(new Point(0, 0)); return new Point(mouse.X - offset.X, mouse.Y - offset.Y); }
// Transform in relation to the root SurfaceWindow public static GeneralTransform GetTransformGlobal(Visual visual) { SurfaceWindow window = GetSurfaceWindow(visual); if (window != null) { try { return visual.TransformToAncestor(window); } catch (InvalidOperationException) { return null; } } return null; //return visual.TransformToAncestor(Application.Current.MainWindow); }
public static Point GetMousePosition(Visual relativeTo) { Win32Point mouse = new Win32Point(); GetCursorPos(ref mouse); System.Windows.Interop.HwndSource presentationSource = (System.Windows.Interop.HwndSource)PresentationSource.FromVisual(relativeTo); ScreenToClient(presentationSource.Handle, ref mouse); GeneralTransform transform = relativeTo.TransformToAncestor(presentationSource.RootVisual); Point offset = transform.Transform(new Point(0, 0)); // // Point p = new Point(mouse.X - offset.X, mouse.Y - offset.Y); // System.Diagnostics.Debug.WriteLine(string.Format("mouse {0:0.0}|{1:0.0} offset {2:0.0}|{3:0.0} res {4:0.0}|{5:0.0}", // mouse.X, mouse.Y, offset.X, offset.Y, p.X, p.Y)); return new Point(mouse.X - offset.X, mouse.Y - offset.Y); }
public static Point TransformToScreen(Point point, Visual relativeTo) { var hwndSource = PresentationSource.FromVisual(relativeTo) as HwndSource; var root = hwndSource.RootVisual; // Translate the point from the visual to the root. var transformToRoot = relativeTo.TransformToAncestor(root); var pointRoot = transformToRoot.Transform(point); // Transform the point from the root to client coordinates. var m = Matrix.Identity; var transform = VisualTreeHelper.GetTransform(root); if (transform != null) { m = Matrix.Multiply(m, transform.Value); } var offset = VisualTreeHelper.GetOffset(root); m.Translate(offset.X, offset.Y); var pointClient = m.Transform(pointRoot); // Convert from “device-independent pixels” into pixels. pointClient = hwndSource.CompositionTarget.TransformToDevice.Transform(pointClient); var pointClientPixels = new Native.POINT(); pointClientPixels.x = (0 < pointClient.X) ? (int)(pointClient.X + 0.5) : (int)(pointClient.X - 0.5); pointClientPixels.y = (0 < pointClient.Y) ? (int)(pointClient.Y + 0.5) : (int)(pointClient.Y - 0.5); // Transform the point into screen coordinates. var pointScreenPixels = pointClientPixels; Native.ClientToScreen(hwndSource.Handle, ref pointScreenPixels); //Native.GetCurrentPositionEx(hwndSource.Handle, out pointScreenPixels); //Native.GetWindowOrgEx(hwndSource.Handle, out pointScreenPixels); return new Point(pointScreenPixels.x, pointScreenPixels.y); }
/// <summary> /// Gets transform to ancestor for inner scope /// </summary> private Transform GetTransformToAncestor(Visual innerScope) { // NOTE: TransformToAncestor is safe (will never throw an exception). Transform transform = innerScope.TransformToAncestor(_renderScope) as Transform; if (transform == null) { transform = Transform.Identity; } return transform; }
private Point ScreenToClient(Point point, Visual visual) { PresentationSource presentationSource = PresentationSource.CriticalFromVisual(visual); point = PointUtil.ScreenToClient(point, presentationSource); if (presentationSource != null) { GeneralTransform transform = visual.TransformToAncestor(presentationSource.RootVisual); if (transform != null) { transform = transform.Inverse; if (transform != null) { point = transform.Transform(point); } } } return point; }
private Point ClientToScreen(Point point, Visual visual) { PresentationSource presentationSource = PresentationSource.CriticalFromVisual(visual); if (presentationSource != null) { GeneralTransform transform = visual.TransformToAncestor(presentationSource.RootVisual); if (transform != null) { point = transform.Transform(point); } } return PointUtil.ClientToScreen(point, presentationSource); }
private void PrepareAttributes(InputScope inputScope, double fontSize, FontFamily fontFamily, XmlLanguage language, Visual visual, int count, Guid[] filterAttributes) { if (_preparedattributes == null) { _preparedattributes = new ArrayList(count); } else { _preparedattributes.Clear(); } int i; for (i = 0; i < _supportingattributes.Length; i++) { if (count != 0) { int j; bool found = false; for (j = 0; j < count; j++) { if (_supportingattributes[i].Guid.Equals(filterAttributes[j])) found = true; } if (!found) continue; } UnsafeNativeMethods.TS_ATTRVAL attrval = new UnsafeNativeMethods.TS_ATTRVAL(); attrval.attributeId = _supportingattributes[i].Guid; attrval.overlappedId = (int)_supportingattributes[i].Style; attrval.val = new NativeMethods.VARIANT(); // This VARIANT is returned to the caller, which supposed to call VariantClear(). // GC does not have to clear it. attrval.val.SuppressFinalize(); switch (_supportingattributes[i].Style) { case AttributeStyle.InputScope: object obj = new InputScopeAttribute(inputScope); attrval.val.vt = (short)NativeMethods.tagVT.VT_UNKNOWN; attrval.val.data1.Value = Marshal.GetIUnknownForObject(obj); break; case AttributeStyle.Font_Style_Height: // We always evaluate the font size and returns a value. attrval.val.vt = (short)NativeMethods.tagVT.VT_I4; attrval.val.data1.Value = (IntPtr)(int)fontSize; break; case AttributeStyle.Font_FaceName: { string familyName = GetFontFamilyName(fontFamily, language); if (familyName != null) { attrval.val.vt = (short)NativeMethods.tagVT.VT_BSTR; attrval.val.data1.Value = Marshal.StringToBSTR(familyName); } } break; case AttributeStyle.Font_SizePts: attrval.val.vt = (short)NativeMethods.tagVT.VT_I4; attrval.val.data1.Value = (IntPtr)(int)(fontSize / 96.0 * 72.0); break; case AttributeStyle.Text_ReadOnly: attrval.val.vt = (short)NativeMethods.tagVT.VT_BOOL; attrval.val.data1.Value = IsReadOnly ? (IntPtr)1 : (IntPtr)0; break; case AttributeStyle.Text_Orientation: attrval.val.vt = (short)NativeMethods.tagVT.VT_I4; attrval.val.data1.Value = (IntPtr)0; // Get the transformation that is relative from source. PresentationSource source = null; source = PresentationSource.CriticalFromVisual((Visual)RenderScope); if (source != null) { Visual root = source.RootVisual; if ((root != null) && (visual != null)) { // // Calc radian from Matirix. This is approximate calculation from the first row. // If tf.M12 is 0, angle will be 0. So we don't have to calc it. // GeneralTransform transform = visual.TransformToAncestor(root); Transform t = transform.AffineTransform; // if (t != null) { Matrix tf = t.Value; if ((tf.M11 != 0) || (tf.M12 != 0)) { double radSin = Math.Asin(tf.M12 / Math.Sqrt((tf.M11 * tf.M11) + (tf.M12 * tf.M12))); double radCos = Math.Acos(tf.M11 / Math.Sqrt((tf.M11 * tf.M11) + (tf.M12 * tf.M12))); // double angleSin = Math.Round((radSin * 180) / Math.PI, 0); double angleCos = Math.Round((radCos * 180) / Math.PI, 0); double angle; // determine angle from the sign of radSin; if (radSin <= 0) angle = angleCos; else angle = 360 - angleCos; attrval.val.data1.Value = (IntPtr)((int)angle * 10); } } } } break; case AttributeStyle.Text_VerticalWriting: // // attrval.val.vt = (short)NativeMethods.tagVT.VT_BOOL; attrval.val.data1.Value = (IntPtr)0; break; } _preparedattributes.Add(attrval); } }
/// <summary> /// Calculates visible rectangle taking into account all clips and transforms /// in the visual ancestors chain. /// </summary> /// <param name="visibleRect">Original rectangle relative to 'visual'.</param> /// <param name="originalVisual">Originating visual element.</param> internal static Rect CalculateVisibleRect(Rect visibleRect, Visual originalVisual) { Visual visual = VisualTreeHelper.GetParent(originalVisual) as Visual; while (visual != null && visibleRect != Rect.Empty) { if (VisualTreeHelper.GetClip(visual) != null) { GeneralTransform transform = originalVisual.TransformToAncestor(visual).Inverse; // Safer version of transform to descendent (doing the inverse ourself), // we want the rect inside of our space. (Which is always rectangular and much nicer to work with) if (transform != null) { Rect rectBounds = VisualTreeHelper.GetClip(visual).Bounds; rectBounds = transform.TransformBounds(rectBounds); visibleRect.Intersect(rectBounds); } else { // No visibility if non-invertable transform exists. visibleRect = Rect.Empty; } } visual = VisualTreeHelper.GetParent(visual) as Visual; } return visibleRect; }
/// <summary> /// StackPanel implementation of <seealso cref="IScrollInfo.MakeVisible" />. /// </summary> // The goal is to change offsets to bring the child into view, and return a rectangle in our space to make visible. // The rectangle we return is in the physical dimension the input target rect transformed into our pace. // In the logical dimension, it is our immediate child's rect. // Note: This code presently assumes we/children are layout clean. See work item 22269 for more detail. public Rect MakeVisible(Visual visual, Rect rectangle) { Vector newOffset = new Vector(); Rect newRect = new Rect(); // We can only work on visuals that are us or children. // An empty rect has no size or position. We can't meaningfully use it. if ( rectangle.IsEmpty || visual == null || visual == (Visual)this || !this.IsAncestorOf(visual)) { return Rect.Empty; } #pragma warning disable 1634, 1691 #pragma warning disable 56506 // Compute the child's rect relative to (0,0) in our coordinate space. // This is a false positive by PreSharp. visual cannot be null because of the 'if' check above GeneralTransform childTransform = visual.TransformToAncestor(this); #pragma warning restore 56506 #pragma warning restore 1634, 1691 rectangle = childTransform.TransformBounds(rectangle); // We can't do any work unless we're scrolling. if (!IsScrolling) { return rectangle; } // Bring the target rect into view in the physical dimension. MakeVisiblePhysicalHelper(rectangle, ref newOffset, ref newRect); // Bring our child containing the visual into view. int childIndex = FindChildIndexThatParentsVisual(visual); MakeVisibleLogicalHelper(childIndex, ref newOffset, ref newRect); // We have computed the scrolling offsets; validate and scroll to them. newOffset.X = ScrollContentPresenter.CoerceOffset(newOffset.X, _scrollData._extent.Width, _scrollData._viewport.Width); newOffset.Y = ScrollContentPresenter.CoerceOffset(newOffset.Y, _scrollData._extent.Height, _scrollData._viewport.Height); if (!DoubleUtil.AreClose(newOffset, _scrollData._offset)) { _scrollData._offset = newOffset; InvalidateMeasure(); OnScrollChange(); } // Return the rectangle return newRect; }
public Rect MakeVisible(Visual visual, Rect rectangle) { if (rectangle.IsEmpty || visual == null || visual == this || !IsAncestorOf(visual)) { return Rect.Empty; } rectangle = visual.TransformToAncestor(this).TransformBounds(rectangle); var viewRect = new Rect(HorizontalOffset, VerticalOffset, ViewportWidth, ViewportHeight); rectangle.X += viewRect.X; rectangle.Y += viewRect.Y; viewRect.X = CalculateNewScrollOffset(viewRect.Left, viewRect.Right, rectangle.Left, rectangle.Right); viewRect.Y = CalculateNewScrollOffset(viewRect.Top, viewRect.Bottom, rectangle.Top, rectangle.Bottom); SetHorizontalOffset(viewRect.X); SetVerticalOffset(viewRect.Y); rectangle.Intersect(viewRect); rectangle.X -= viewRect.X; rectangle.Y -= viewRect.Y; return rectangle; }
/// <summary> /// <see cref="IScrollInfo.MakeVisible"/> /// </summary> internal Rect MakeVisible(UIElement owner, Visual visual, Rect rectangle) { // We can only work on visuals that are us or children. // An empty rect has no size or position. We can't meaningfully use it. if (rectangle.IsEmpty || visual == null || (visual != owner && !owner.IsAncestorOf(visual))) { return Rect.Empty; } // Compute the child's rect relative to (0,0) in our coordinate space. GeneralTransform childTransform = visual.TransformToAncestor(owner); rectangle = childTransform.TransformBounds(rectangle); // Initialize the viewport Rect viewport = new Rect(_offset.X, _offset.Y, _viewport.Width, _viewport.Height); rectangle.X += viewport.X; rectangle.Y += viewport.Y; // Compute the offsets required to minimally scroll the child maximally into view. double minX = ComputeScrollOffset(viewport.Left, viewport.Right, rectangle.Left, rectangle.Right); double minY = ComputeScrollOffset(viewport.Top, viewport.Bottom, rectangle.Top, rectangle.Bottom); // We have computed the scrolling offsets; scroll to them. SetHorizontalOffset(owner, minX); SetVerticalOffset(owner, minY); // Compute the visible rectangle of the child relative to the viewport. if (this.CanHorizontallyScroll) { viewport.X = minX; } else { // munge the intersection rectangle.X = viewport.X; } if (this.CanVerticallyScroll) { viewport.Y = minY; } else { // munge the intersection rectangle.Y = viewport.Y; } rectangle.Intersect(viewport); if (!rectangle.IsEmpty) { rectangle.X -= viewport.X; rectangle.Y -= viewport.Y; } // Return the rectangle return rectangle; }
/// <summary> /// Bring the specified rectangle to view. /// </summary> public Rect MakeVisible(Visual visual, Rect rectangle) { if (visual == null) { throw new ArgumentNullResourceException("visual", Properties.Resources.General_Given_Parameter_Cannot_Be_Null); } if (this.content.IsAncestorOf(visual)) { Rect transformedRect = visual.TransformToAncestor(this.content).TransformBounds(rectangle); var viewportRect = new Rect(ContentOffsetX, ContentOffsetY, ContentViewportWidth, ContentViewportHeight); if (!transformedRect.Contains(viewportRect)) { double horizOffset = 0; double vertOffset = 0; if (transformedRect.Left < viewportRect.Left) { // // Want to move viewport left. // horizOffset = transformedRect.Left - viewportRect.Left; } else if (transformedRect.Right > viewportRect.Right) { // // Want to move viewport right. // horizOffset = transformedRect.Right - viewportRect.Right; } if (transformedRect.Top < viewportRect.Top) { // // Want to move viewport up. // vertOffset = transformedRect.Top - viewportRect.Top; } else if (transformedRect.Bottom > viewportRect.Bottom) { // // Want to move viewport down. // vertOffset = transformedRect.Bottom - viewportRect.Bottom; } SnapContentOffsetTo(new Point(ContentOffsetX + horizOffset, ContentOffsetY + vertOffset)); } } return rectangle; }
/// <summary> /// Get relative offset /// </summary> /// <param name="child">child visual</param> /// <param name="parent">parent visual</param> /// <returns>the relative offset</returns> public static Point GetRelativeOffset(Visual child, Visual parent) { Contract.Requires(child != null); Contract.Requires(parent != null); Point relativePoint = child.TransformToAncestor(parent) .Transform(new Point(0, 0)); return relativePoint; }
public Rect MakeVisible( Visual visual, Rect rectangle ) { if( this.ActiveLayout != null && this.ActiveLayout is IScrollInfo ) return ( ( IScrollInfo )this.ActiveLayout ).MakeVisible( visual, rectangle ); if( ( rectangle.IsEmpty || ( visual == null ) ) || ( ( visual == this ) || !this.IsAncestorOf( visual ) ) ) return Rect.Empty; rectangle = visual.TransformToAncestor( ( Visual )this ).TransformBounds( rectangle ); if( this.IsScrollingPhysically == false ) return rectangle; // // Make sure we can find the child... // int index = this.FindChildFromVisual( visual ); if( index == -1 ) throw new ArgumentException( "visual" ); // // Since our _Offset pushes the items down we need to correct it here to // give a true rectangle of the child. // Rect itemRect = rectangle; itemRect.Offset( _offset ); Rect viewRect = new Rect( new Point( _offset.X, _offset.Y ), _viewport ); Vector newPhysOffset; if( ScrollHelper.ScrollLeastAmount( viewRect, itemRect, out newPhysOffset ) ) { this.SetHorizontalOffset( newPhysOffset.X ); this.SetVerticalOffset( newPhysOffset.Y ); } return rectangle; }
public Rect MakeVisible(Visual visual, Rect rectangle) { if (rectangle.IsEmpty || visual == null || visual == this || !base.IsAncestorOf(visual)) { return Rect.Empty; } TreeViewExItem treeViewExItem = visual as TreeViewExItem; FrameworkElement element; if (treeViewExItem != null) { element = treeViewExItem.Template.FindName("border", treeViewExItem) as FrameworkElement; } else { element = visual as FrameworkElement; } var transform = visual.TransformToAncestor(this); Point p = transform.Transform(new Point(0, 0)); Rect rect = new Rect(p, element.RenderSize); if (rect.X < 0) { SetHorizontalOffset(HorizontalOffset + rect.X); } else if (treeViewExItem != null && treeViewExItem.ParentTreeView.ActualWidth < rect.X) { SetHorizontalOffset(HorizontalOffset + rect.X); } if (rect.Y < 0) { SetVerticalOffset(VerticalOffset + rect.Y); } else if (treeViewExItem != null && treeViewExItem.ParentTreeView.ActualHeight < rect.Y + rect.Height) { // set 5 more, so the next item is realized for sure. double verticalOffset = rect.Y + rect.Height + VerticalOffset - treeViewExItem.ParentTreeView.ActualHeight + 5; SetVerticalOffset(verticalOffset); } return new Rect(HorizontalOffset, VerticalOffset, ViewportWidth, ViewportHeight); }
/// <summary> /// Transforms rectangle from inner scope. /// </summary> private Rect TransformToAncestor(Visual innerScope, Rect rect) { if (rect != Rect.Empty) { // NOTE: TransformToAncestor is safe (will never throw an exception). GeneralTransform transform = innerScope.TransformToAncestor(_renderScope); if (transform != null) { rect = transform.TransformBounds(rect); } } return rect; }
/// <summary> /// Bring the specified rectangle to view. /// </summary> public Rect MakeVisible(Visual visual, Rect rectangle) { if (content.IsAncestorOf(visual)) { Rect transformedRect = visual.TransformToAncestor(content).TransformBounds(rectangle); if (!transformedRect.IntersectsWith(new Rect(ContentOffsetX, ContentOffsetY, ContentViewportWidth, ContentViewportHeight))) { AnimatedSnapTo(new Point(transformedRect.X + (transformedRect.Width / 2), transformedRect.Y + (transformedRect.Height / 2))); } } return rectangle; }
/// <summary> /// Transforms rectangle from inner scope /// </summary> private Point TransformToDescendant(Visual innerScope, Point point) { // NOTE: TransformToAncestor is safe (will never throw an exception). GeneralTransform transform = innerScope.TransformToAncestor(_renderScope); if (transform != null) { transform = transform.Inverse; if (transform != null) { point = transform.Transform(point); } } return point; }
Rect IScrollInfo.MakeVisible(Visual visual, Rect rectangle) { if (rectangle.IsEmpty || visual == null || !IsAncestorOf(visual)) { return Rect.Empty; } rectangle = visual.TransformToAncestor(this).TransformBounds(rectangle); rectangle = RenderTransform.TransformBounds(rectangle); var width = ((IScrollInfo)this).ViewportWidth; var height = ((IScrollInfo)this).ViewportHeight; var left = -rectangle.X; var right = left + width - rectangle.Width; var top = -rectangle.Y; var bottom = top + height - rectangle.Height; var deltaX = left > 0 && right > 0 ? Math.Min(left, right) : left < 0 && right < 0 ? Math.Max(left, right) : 0.0; var deltaY = top > 0 && bottom > 0 ? Math.Min(top, bottom) : top < 0 && bottom < 0 ? Math.Max(top, bottom) : 0.0; var offset = Offset; offset.X -= deltaX; offset.Y -= deltaY; Offset = offset; rectangle.X += deltaX; rectangle.Y += deltaY; rectangle.Intersect(new Rect(0, 0, width, height)); return rectangle; }
public Rect MakeVisible(Visual visual, Rect rectangle) { if (rectangle.IsEmpty || visual == null || visual == this || !base.IsAncestorOf(visual)) { return Rect.Empty; } FrameworkElement fe = visual as FrameworkElement; var transform = visual.TransformToAncestor(this); Point p = transform.Transform(new Point(0, 0)); p.Offset(0, trans.Y); Rect rect = new Rect(p, fe.RenderSize); if (rect.Y < 0) { SetVerticalOffset(VerticalOffset + rect.Y); } else if (rect.Y + rect.Height > viewport.Height) { double verticalOffset = rect.Y + rect.Height + VerticalOffset - viewport.Height; SetVerticalOffset(verticalOffset); } return new Rect(HorizontalOffset, VerticalOffset, ViewportWidth, ViewportHeight); }
/// <summary> /// Calculates a TextPointer indended for dropping the text. /// </summary> /// <param name="target"></param> /// <param name="point"></param> /// <returns> /// ITextPointer intended for dropping the selected text. /// Adjusts the dropping point to a word boundary (beginning of word) /// in case if source range contains whole words. /// The position returned is oriented towards a character /// under the mouse pointer. /// </returns> private ITextPointer GetDropPosition(Visual target, Point point) { Invariant.Assert(target != null); Invariant.Assert(_textEditor.TextView.IsValid); // caller must guarantee this. // Convert point to RenderScope if (target != _textEditor.TextView.RenderScope && target != null && (_textEditor.TextView.RenderScope).IsAncestorOf(target)) { GeneralTransform transform = target.TransformToAncestor(_textEditor.TextView.RenderScope); transform.TryTransform(point, out point); } ITextPointer dropPosition = this.TextView.GetTextPositionFromPoint(point, /*snapToText:*/true); // For rich text content we adjust drop position to word boundary if (dropPosition != null) { // Normalize drop position dropPosition = dropPosition.GetInsertionPosition(dropPosition.LogicalDirection); if (_textEditor.AcceptsRichContent) { TextSegment lineRange = TextEditorSelection.GetNormalizedLineRange(this.TextView, dropPosition); if (!lineRange.IsNull && // The drop position must be before of end of line dropPosition.CompareTo(lineRange.End) < 0 && // We check if we are not at word boundary already: !TextPointerBase.IsAtWordBoundary(dropPosition, /*insideWordDirection:*/LogicalDirection.Forward) && // We do not do it if the source range was not on word boundaries from both ends _dragSourceTextRange != null && // TextPointerBase.IsAtWordBoundary(_dragSourceTextRange.Start, LogicalDirection.Forward) && // TextPointerBase.IsAtWordBoundary(_dragSourceTextRange.End, LogicalDirection.Forward)) { // Move to word boundary. Select closest one to a dropPosition. TextSegment wordSegment = TextPointerBase.GetWordRange(dropPosition); string wordText = TextRangeBase.GetTextInternal(wordSegment.Start, wordSegment.End); int indexInWord = wordSegment.Start.GetOffsetToPosition(dropPosition); dropPosition = (indexInWord < (wordText.Length / 2)) ? wordSegment.Start : wordSegment.End; } } } return dropPosition; }
// ------------------------------------------------------------------ // // Basic UIElement/Visual Dump // // ------------------------------------------------------------------ #region Basic UIElement/Visual Dump // ------------------------------------------------------------------ // Dump content of Visual. // ------------------------------------------------------------------ internal static void DumpVisual(XmlTextWriter writer, Visual visual, Visual parent) { if (visual is UIElement) { DumpUIElement(writer, (UIElement)visual, parent, false); } else { writer.WriteStartElement(visual.GetType().Name); // Dump visual bounds Rect bounds = visual.VisualContentBounds; if(!bounds.IsEmpty) { DumpRect(writer, "ContentRect", bounds); } // Dump clip geometry Geometry clip = VisualTreeHelper.GetClip(visual); if (clip != null) { DumpRect(writer, "Clip.Bounds", clip.Bounds); } // Dump transform relative to its parent GeneralTransform g = visual.TransformToAncestor(parent); Point point = new Point(0, 0); g.TryTransform(point, out point); if (point.X != 0 || point.Y != 0) { DumpPoint(writer, "Position", point); } // Dump visual children DumpVisualChildren(writer, "Children", visual); writer.WriteEndElement(); } }
/// <summary> /// Bring the specified rectangle to view. /// </summary> public Rect MakeVisible(Visual visual, Rect rectangle) { if (content.IsAncestorOf(visual)) { Rect transformedRect = visual.TransformToAncestor(content).TransformBounds(rectangle); Rect viewportRect = new Rect(ContentOffsetX, ContentOffsetY, ContentViewportWidth, ContentViewportHeight); if (!transformedRect.Contains(viewportRect)) { double horizOffset = 0; double vertOffset = 0; if (transformedRect.Left < viewportRect.Left) { // // Want to move viewport left. // horizOffset = transformedRect.Left - viewportRect.Left; } else if (transformedRect.Right > viewportRect.Right) { // // Want to move viewport right. // horizOffset = transformedRect.Right - viewportRect.Right; } if (transformedRect.Top < viewportRect.Top) { // // Want to move viewport up. // vertOffset = transformedRect.Top - viewportRect.Top; } else if (transformedRect.Bottom > viewportRect.Bottom) { // // Want to move viewport down. // vertOffset = transformedRect.Bottom - viewportRect.Bottom; } SnapContentOffsetTo(new Point(ContentOffsetX + horizOffset, ContentOffsetY + vertOffset)); } } return rectangle; }
public Rect MakeVisible(Visual visual, Rect rectangle) { if (rectangle.IsEmpty || visual == null || visual == this || !base.IsAncestorOf(visual)) { return Rect.Empty; } rectangle = visual.TransformToAncestor(this).TransformBounds(rectangle); return MakeRectVisible(rectangle); }
/// <summary> /// Convert a point from the coordinate space of the specified /// element into the "client" coordinate space of the window. /// </summary> public static Point TransformDescendantToClient(this PresentationSource presentationSource, Point point, Visual descendant) { Point pt = descendant.TransformToAncestor(presentationSource.RootVisual).Transform(point); return TransformRootToClient(presentationSource, pt); }
/// <summary> /// Forces content to scroll until the coordinate space of a System.Windows.Media.Visual object is visible. /// This is optimized for horizontal scrolling only /// </summary> /// <param name="visual">A System.Windows.Media.Visual that becomes visible.</param> /// <param name="rectangle">A bounding rectangle that identifies the coordinate space to make visible.</param> /// <returns>A System.Windows.Rect that is visible.</returns> public Rect MakeVisible(Visual visual, Rect rectangle) { // We can only work on visuals that are us or children. // An empty rect has no size or position. We can't meaningfully use it. if (rectangle.IsEmpty || visual == null || ReferenceEquals(visual, this) || !this.IsAncestorOf(visual)) { return Rect.Empty; } // Compute the child's rect relative to (0,0) in our coordinate space. GeneralTransform childTransform = visual.TransformToAncestor(this); rectangle = childTransform.TransformBounds(rectangle); // Initialize the viewport Rect viewport = new Rect(HorizontalOffset, rectangle.Top, ViewportWidth, rectangle.Height); rectangle.X += viewport.X; // Compute the offsets required to minimally scroll the child maximally into view. double minX = ComputeScrollOffsetWithMinimalScroll(viewport.Left, viewport.Right, rectangle.Left, rectangle.Right); // We have computed the scrolling offsets; scroll to them. SetHorizontalOffset(minX); // Compute the visible rectangle of the child relative to the viewport. viewport.X = minX; rectangle.Intersect(viewport); rectangle.X -= viewport.X; // Return the rectangle return rectangle; }
private Point GetPosition(Visual element) { var positionTransform = element.TransformToAncestor(this); var areaPosition = positionTransform.Transform(new Point(0, 0)); return areaPosition; }