internal override Rect GetVisibleBoundingRectCore()
        {
            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(_owner);

            // If there's no source, the element is not visible, return empty rect
            if (presentationSource == null)
            {
                return(Rect.Empty);
            }

            HwndSource hwndSource = presentationSource as HwndSource;

            // If the source isn't an HwnSource, there's not much we can do, return empty rect
            if (hwndSource == null)
            {
                return(Rect.Empty);
            }

            Rect rectElement = CalculateVisibleBoundingRect(_owner);
            Rect rectRoot    = PointUtil.ElementToRoot(rectElement, _owner, presentationSource);
            Rect rectClient  = PointUtil.RootToClient(rectRoot, presentationSource);
            Rect rectScreen  = PointUtil.ClientToScreen(rectClient, hwndSource);

            return(rectScreen);
        }
Ejemplo n.º 2
0
        private bool ComputeBoundingRectangle(out Rect rect)
        {
            rect = Rect.Empty;

            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(_owner);

            // If there's no source, the element is not visible, return empty rect
            if (presentationSource == null)
            {
                return(false);
            }

            HwndSource hwndSource = presentationSource as HwndSource;

            // If the source isn't an HwndSource, there's not much we can do, return empty rect
            if (hwndSource == null)
            {
                return(false);
            }

            Rect rectElement = _owner.Visual2DContentBounds;
            // we use VisualTreeHelper.GetContainingVisual2D to transform from the containing Viewport3DVisual
            Rect rectRoot   = PointUtil.ElementToRoot(rectElement, VisualTreeHelper.GetContainingVisual2D(_owner), presentationSource);
            Rect rectClient = PointUtil.RootToClient(rectRoot, presentationSource);

            rect = PointUtil.ClientToScreen(rectClient, hwndSource);

            return(true);
        }
        override protected Point GetClickablePointCore()
        {
            Point pt = new Point(double.NaN, double.NaN);

            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(_owner);

            // If there's no source, the element is not visible, return (double.NaN, double.NaN) point
            if (presentationSource == null)
            {
                return(pt);
            }

            HwndSource hwndSource = presentationSource as HwndSource;

            // If the source isn't an HwnSource, there's not much we can do, return (double.NaN, double.NaN) point
            if (hwndSource == null)
            {
                return(pt);
            }

            Rect rectElement = new Rect(new Point(0, 0), _owner.RenderSize);
            Rect rectRoot    = PointUtil.ElementToRoot(rectElement, _owner, presentationSource);
            Rect rectClient  = PointUtil.RootToClient(rectRoot, presentationSource);
            Rect rectScreen  = PointUtil.ClientToScreen(rectClient, hwndSource);

            pt = new Point(rectScreen.Left + rectScreen.Width * 0.5, rectScreen.Top + rectScreen.Height * 0.5);

            return(pt);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Move the mouse to the specified position within the given element.  x and y are
        /// coordinates within the element, (0,0) is upper left, (1,1) is lower right.
        /// </summary>
        /// <param name="target"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public bool MoveMouse(UIElement target, double x, double y)
        {
            // This code is paraphrased from Popup.cs.

            PresentationSource source = PresentationSource.FromVisual(target);
            if (source == null) return false;

            // Transform (0,0) from the target element up to the root.
            Point ptTarget = new Point(0,0);
            GeneralTransform transform;
            try
            {
                transform = target.TransformToAncestor(source.RootVisual);
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine( "  MoveMouse to ({0},{1}) of {2} failed",
                                   x, y, Identify( target ) );
                // if visuals are not connected...
                return false;
            }
            Point ptRoot;
            transform.TryTransform(ptTarget, out ptRoot);
            Point ptClient = PointUtil.RootToClient(ptRoot, source);
            Point ptScreen = PointUtil.ClientToScreen(ptClient, source);
            
            // Get the width of the target element in pixels.
            Point size = source.CompositionTarget.TransformToDevice.Transform(new Point(target.RenderSize.Width, target.RenderSize.Height));

            Point moveToPoint = new Point(ptScreen.X + size.X * x, ptScreen.Y + size.Y * y);

            MoveMouse( moveToPoint );

            return true;
        }
        override protected Rect GetBoundingRectangleCore()
        {
            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(_owner);

            // If there's no source, the element is not visible, return empty rect
            if (presentationSource == null)
            {
                return(Rect.Empty);
            }

            HwndSource hwndSource = presentationSource as HwndSource;

            // If the source isn't an HwnSource, there's not much we can do, return empty rect
            if (hwndSource == null)
            {
                return(Rect.Empty);
            }

            Rect rectElement = new Rect(new Point(0, 0), _owner.RenderSize);
            Rect rectRoot    = PointUtil.ElementToRoot(rectElement, _owner, presentationSource);
            Rect rectClient  = PointUtil.RootToClient(rectRoot, presentationSource);
            Rect rectScreen  = PointUtil.ClientToScreen(rectClient, hwndSource);

            return(rectScreen);
        }
Ejemplo n.º 6
0
        protected override Rect GetBoundingRectangleCore()
        {
            TextElement textElement = (TextElement)base.Owner;
            ITextView   textView    = textElement.TextContainer.TextView;

            if (textView == null || !textView.IsValid)
            {
                return(Rect.Empty);
            }
            Geometry tightBoundingGeometryFromTextPositions = textView.GetTightBoundingGeometryFromTextPositions(textElement.ContentStart, textElement.ContentEnd);

            if (tightBoundingGeometryFromTextPositions == null)
            {
                return(Rect.Empty);
            }
            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(textView.RenderScope);

            if (presentationSource == null)
            {
                return(Rect.Empty);
            }
            HwndSource hwndSource = presentationSource as HwndSource;

            if (hwndSource == null)
            {
                return(Rect.Empty);
            }
            Rect bounds     = tightBoundingGeometryFromTextPositions.Bounds;
            Rect rectRoot   = PointUtil.ElementToRoot(bounds, textView.RenderScope, presentationSource);
            Rect rectClient = PointUtil.RootToClient(rectRoot, presentationSource);

            return(PointUtil.ClientToScreen(rectClient, hwndSource));
        }
Ejemplo n.º 7
0
        private NativeMethods.RECT CalculateAssignedRC(PresentationSource source)
        {
            Rect   rectElement = new Rect(base.RenderSize);
            Rect   rectRoot    = PointUtil.ElementToRoot(rectElement, this, source);
            Rect   rect        = PointUtil.RootToClient(rectRoot, source);
            IntPtr parent      = UnsafeNativeMethods.GetParent(this._hwnd);

            NativeMethods.RECT rc    = PointUtil.FromRect(rect);
            NativeMethods.RECT rect2 = PointUtil.AdjustForRightToLeft(rc, new HandleRef(null, parent));
            if (!CoreAppContextSwitches.DoNotUsePresentationDpiCapabilityTier2OrGreater)
            {
                rect2 = this.AdjustRectForDpi(rect2);
            }
            return(rect2);
        }
        internal static Point ClientToScreen(UIElement relativeTo, Point point)
        {
            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(relativeTo);

            if (presentationSource == null)
            {
                return(new Point(double.NaN, double.NaN));
            }
            GeneralTransform generalTransform = relativeTo.TransformToAncestor(presentationSource.RootVisual);
            Point            point2;

            generalTransform.TryTransform(point, out point2);
            Point pointClient = PointUtil.RootToClient(point2, presentationSource);

            return(PointUtil.ClientToScreen(pointClient, presentationSource));
        }
        protected override Rect GetBoundingRectangleCore()
        {
            UIElement uielement;
            Rect      rect = this.CalculateBoundingRect(false, out uielement);

            if (rect != Rect.Empty && uielement != null)
            {
                HwndSource hwndSource = PresentationSource.CriticalFromVisual(uielement) as HwndSource;
                if (hwndSource != null)
                {
                    rect = PointUtil.ElementToRoot(rect, uielement, hwndSource);
                    rect = PointUtil.RootToClient(rect, hwndSource);
                    rect = PointUtil.ClientToScreen(rect, hwndSource);
                }
            }
            return(rect);
        }
Ejemplo n.º 10
0
        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 Point GetClickablePointCore()
        {
            Point     result = default(Point);
            UIElement uielement;
            Rect      rect = this.CalculateBoundingRect(true, out uielement);

            if (rect != Rect.Empty && uielement != null)
            {
                HwndSource hwndSource = PresentationSource.CriticalFromVisual(uielement) as HwndSource;
                if (hwndSource != null)
                {
                    rect   = PointUtil.ElementToRoot(rect, uielement, hwndSource);
                    rect   = PointUtil.RootToClient(rect, hwndSource);
                    rect   = PointUtil.ClientToScreen(rect, hwndSource);
                    result = new Point(rect.Left + rect.Width * 0.1, rect.Top + rect.Height * 0.1);
                }
            }
            return(result);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// <see cref="AutomationPeer.GetClickablePointCore"/>
        /// </summary>
        protected override Point GetClickablePointCore()
        {
            Point     point = new Point();
            UIElement uiScope;
            Rect      boundingRect = CalculateBoundingRect(true, out uiScope);

            if (boundingRect != Rect.Empty && uiScope != null)
            {
                HwndSource hwndSource = PresentationSource.CriticalFromVisual(uiScope) as HwndSource;
                if (hwndSource != null)
                {
                    boundingRect = PointUtil.ElementToRoot(boundingRect, uiScope, hwndSource);
                    boundingRect = PointUtil.RootToClient(boundingRect, hwndSource);
                    boundingRect = PointUtil.ClientToScreen(boundingRect, hwndSource);
                    point        = new Point(boundingRect.Left + boundingRect.Width * 0.1, boundingRect.Top + boundingRect.Height * 0.1);
                }
            }
            return(point);
        }
Ejemplo n.º 13
0
        protected override Point GetClickablePointCore()
        {
            Point pt = new Point();

            TextElement textElement = (TextElement)Owner;
            ITextView   textView    = textElement.TextContainer.TextView;

            if (textView == null || !textView.IsValid || (!textView.Contains(textElement.ContentStart) && !textView.Contains(textElement.ContentEnd)))
            {
                return(pt);
            }

            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(textView.RenderScope);

            if (presentationSource == null)
            {
                return(pt);
            }

            HwndSource hwndSource = presentationSource as HwndSource;

            // If the source isn't an HwnSource, there's not much we can do, return empty rect
            if (hwndSource == null)
            {
                return(pt);
            }

            TextPointer endPosition = textElement.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward);

            if (endPosition == null || endPosition.CompareTo(textElement.ContentEnd) > 0)
            {
                endPosition = textElement.ContentEnd;
            }

            Rect rectElement = CalculateVisibleRect(textView, textElement, textElement.ContentStart, endPosition);
            Rect rectRoot    = PointUtil.ElementToRoot(rectElement, textView.RenderScope, presentationSource);
            Rect rectClient  = PointUtil.RootToClient(rectRoot, presentationSource);
            Rect rectScreen  = PointUtil.ClientToScreen(rectClient, hwndSource);

            pt = new Point(rectScreen.Left + rectScreen.Width * 0.5, rectScreen.Top + rectScreen.Height * 0.5);

            return(pt);
        }
Ejemplo n.º 14
0
        // Translate the layout information assigned to us from the co-ordinate
        // space of this element, through the root visual, to the Win32 client
        // co-ordinate space
        private NativeMethods.RECT CalculateAssignedRC(PresentationSource source)
        {
            Rect rectElement = new Rect(RenderSize);
            Rect rectRoot    = PointUtil.ElementToRoot(rectElement, this, source);
            Rect rectClient  = PointUtil.RootToClient(rectRoot, source);

            // Adjust for Right-To-Left oriented windows
            IntPtr hwndParent = UnsafeNativeMethods.GetParent(_hwnd);

            NativeMethods.RECT rcClient            = PointUtil.FromRect(rectClient);
            NativeMethods.RECT rcClientRTLAdjusted = PointUtil.AdjustForRightToLeft(rcClient, new HandleRef(null, hwndParent));

            if (!CoreAppContextSwitches.DoNotUsePresentationDpiCapabilityTier2OrGreater)
            {
                //Adjust for differences in DPI between _hwnd and hwndParent
                rcClientRTLAdjusted = AdjustRectForDpi(rcClientRTLAdjusted);
            }

            return(rcClientRTLAdjusted);
        }
Ejemplo n.º 15
0
        protected override Rect GetBoundingRectangleCore()
        {
            TextElement textElement = (TextElement)Owner;
            ITextView   textView    = textElement.TextContainer.TextView;

            if (textView == null || !textView.IsValid)
            {
                return(Rect.Empty);
            }

            Geometry geometry = textView.GetTightBoundingGeometryFromTextPositions(textElement.ContentStart, textElement.ContentEnd);

            if (geometry != null)
            {
                PresentationSource presentationSource = PresentationSource.CriticalFromVisual(textView.RenderScope);

                if (presentationSource == null)
                {
                    return(Rect.Empty);
                }

                HwndSource hwndSource = presentationSource as HwndSource;

                // If the source isn't an HwnSource, there's not much we can do, return empty rect
                if (hwndSource == null)
                {
                    return(Rect.Empty);
                }

                Rect rectElement = geometry.Bounds;
                Rect rectRoot    = PointUtil.ElementToRoot(rectElement, textView.RenderScope, presentationSource);
                Rect rectClient  = PointUtil.RootToClient(rectRoot, presentationSource);
                Rect rectScreen  = PointUtil.ClientToScreen(rectClient, hwndSource);

                return(rectScreen);
            }
            else
            {
                return(Rect.Empty);
            }
        }
Ejemplo n.º 16
0
        protected override Point GetClickablePointCore()
        {
            Point       result      = default(Point);
            TextElement textElement = (TextElement)base.Owner;
            ITextView   textView    = textElement.TextContainer.TextView;

            if (textView == null || !textView.IsValid || (!textView.Contains(textElement.ContentStart) && !textView.Contains(textElement.ContentEnd)))
            {
                return(result);
            }
            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(textView.RenderScope);

            if (presentationSource == null)
            {
                return(result);
            }
            HwndSource hwndSource = presentationSource as HwndSource;

            if (hwndSource == null)
            {
                return(result);
            }
            TextPointer textPointer = textElement.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward);

            if (textPointer == null || textPointer.CompareTo(textElement.ContentEnd) > 0)
            {
                textPointer = textElement.ContentEnd;
            }
            Rect rectElement = this.CalculateVisibleRect(textView, textElement, textElement.ContentStart, textPointer);
            Rect rectRoot    = PointUtil.ElementToRoot(rectElement, textView.RenderScope, presentationSource);
            Rect rectClient  = PointUtil.RootToClient(rectRoot, presentationSource);
            Rect rect        = PointUtil.ClientToScreen(rectClient, hwndSource);

            result = new Point(rect.Left + rect.Width * 0.5, rect.Top + rect.Height * 0.5);
            return(result);
        }
Ejemplo n.º 17
0
        internal static Point TranslatePoint(Point pt, DependencyObject from, DependencyObject to, out bool translated)
        {
            translated = false;

            Point ptTranslated = pt;

            // Get the containing and root visuals we are coming from.
            DependencyObject vFromAsDO = InputElement.GetContainingVisual(from);
            Visual           rootFrom  = InputElement.GetRootVisual(from) as Visual;

            Visual vFrom = vFromAsDO as Visual;

            if (vFromAsDO != null && vFrom == null)
            {
                // must be a Visual3D - get it's 2D visual parent
                vFrom = VisualTreeHelper.GetContainingVisual2D(vFromAsDO);
            }

            if (vFrom != null && rootFrom != null)
            {
                GeneralTransform gUp;
                Matrix           mUp;

                bool isUpSimple = false;
                isUpSimple = vFrom.TrySimpleTransformToAncestor(rootFrom,
                                                                false, /* do not apply inverse */
                                                                out gUp,
                                                                out mUp);
                if (isUpSimple)
                {
                    ptTranslated = mUp.Transform(ptTranslated);
                }
                else if (gUp.TryTransform(ptTranslated, out ptTranslated) == false)
                {
                    // Error.  Out parameter has been set false.
                    return(new Point());
                }

                // If no element was specified to translate to, we leave the coordinates
                // translated to the root.
                if (to != null)
                {
                    // Get the containing and root visuals we are going to.
                    DependencyObject vTo    = InputElement.GetContainingVisual(to);
                    Visual           rootTo = InputElement.GetRootVisual(to) as Visual;

                    if (vTo != null && rootTo != null)
                    {
                        // If both are under the same root visual, we can easily translate the point
                        // between them by translating up to the root, and then back down.
                        //
                        // However, if both are under different roots, we can only translate
                        // between them if we know how to relate the two root visuals.  Currently
                        // we only know how to do that if both roots are sourced in HwndSources.
                        if (rootFrom != rootTo)
                        {
                            HwndSource sourceFrom = PresentationSource.CriticalFromVisual(rootFrom) as HwndSource;
                            HwndSource sourceTo   = PresentationSource.CriticalFromVisual(rootTo) as HwndSource;


                            if (sourceFrom != null && sourceFrom.CriticalHandle != IntPtr.Zero && sourceFrom.CompositionTarget != null &&
                                sourceTo != null && sourceTo.CriticalHandle != IntPtr.Zero && sourceTo.CompositionTarget != null)
                            {
                                // Translate the point into client coordinates.
                                ptTranslated = PointUtil.RootToClient(ptTranslated, sourceFrom);

                                // Translate the point into screen coordinates.
                                Point ptScreen = PointUtil.ClientToScreen(ptTranslated, sourceFrom);

                                // Translate the point back the the client coordinates of the To window.
                                ptTranslated = PointUtil.ScreenToClient(ptScreen, sourceTo);

                                // Translate the point back to the root element.
                                ptTranslated = PointUtil.ClientToRoot(ptTranslated, sourceTo);
                            }
                            else
                            {
                                // Error.  Out parameter has been set false.
                                return(new Point());
                            }
                        }

                        // Translate the point from the root to the visual.
                        GeneralTransform gDown;
                        Matrix           mDown;

                        Visual vToAsVisual = vTo as Visual;
                        if (vToAsVisual == null)
                        {
                            // must be a Visual3D
                            vToAsVisual = VisualTreeHelper.GetContainingVisual2D(vTo);
                        }

                        bool isDownSimple = vToAsVisual.TrySimpleTransformToAncestor(rootTo,
                                                                                     true, /* apply inverse */
                                                                                     out gDown,
                                                                                     out mDown);

                        if (isDownSimple)
                        {
                            ptTranslated = mDown.Transform(ptTranslated);
                        }
                        else if (gDown != null)
                        {
                            if (gDown.TryTransform(ptTranslated, out ptTranslated) == false)
                            {
                                // Error.  Out parameter has been set false.
                                return(new Point());
                            }
                        }
                        else
                        {
                            // Error.  Out parameter has been set false.
                            return(new Point());
                        }
                    }
                    else
                    {
                        // Error.  Out parameter has been set false.
                        return(new Point());
                    }
                }
            }
            else
            {
                // Error.  Out parameter has been set false.
                return(new Point());
            }

            translated = true;
            return(ptTranslated);
        }