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); }
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); }
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); } }
/// <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); }
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; }
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); }
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); }
///////////////////////////////////////////////////////////////////// public void SimulateLeftUp(HwndSource window, Point ptClient) { Point ptScreen = PointUtil.ClientToScreen(ptClient, window); Input.SendMouseInput(ptScreen.X, ptScreen.Y, 0, SendMouseInputFlags.LeftUp); }
///////////////////////////////////////////////////////////////////// public void SimulateMove(HwndSource window, Point ptClient) { Point ptScreen = PointUtil.ClientToScreen(ptClient, window); Input.MoveTo(ptScreen); }