{"record":{"id":"1b06ee4aed61fab7","repo":"dotnet/wpf","slug":"sr-screencoordinatesoutsideboundingrect","errorCode":null,"errorMessage":"SR.ScreenCoordinatesOutsideBoundingRect","messagePattern":"SR\\.ScreenCoordinatesOutsideBoundingRect","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/TextPattern.cs","lineNumber":220,"sourceCode":"            return TextPatternRange.Wrap(hTextRange, this);\n        }\n        /// <summary>\n        /// Finds the range nearest to a screen coordinate.\n        /// If the coordinate is within the bounding rectangle of a character then the\n        /// range will contain that character.  Otherwise, it will be a degenerate\n        /// range near the point, chosen in an implementation-dependent manner.\n        /// An InvalidOperation exception is thrown if the point is outside of the\n        /// client area of the text container.\n        /// </summary>\n        /// <param name=\"screenLocation\">The location in screen coordinates.</param>\n        /// <returns>A degenerate range nearest the specified location.</returns>\n        public TextPatternRange RangeFromPoint(Point screenLocation)\n        {\n            //If we are not within the client area throw an exception\n            Rect rect = (Rect)_element.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty);\n            if (screenLocation.X < rect.Left || screenLocation.X >= rect.Right || screenLocation.Y < rect.Top || screenLocation.Y >= rect.Bottom)\n            {\n                throw new ArgumentException(SR.ScreenCoordinatesOutsideBoundingRect);\n            }\n\n            SafeTextRangeHandle hTextRange = UiaCoreApi.TextPattern_RangeFromPoint(_hPattern, screenLocation);\n            return TextPatternRange.Wrap(hTextRange, this);\n        }\n\n        #endregion Public Methods\n        \n        //------------------------------------------------------\n        //\n        //  Public Properties\n        //\n        //------------------------------------------------------\n \n        #region Public Properties\n\n        /// <summary>\n        /// A text range that encloses the main text of the document.  Some auxillary text such as ","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/TextPattern.cs#L202-L238","documentation":"TextPattern.RangeFromPoint throws ArgumentException when the given screen coordinate lies outside the element's current BoundingRectangle. The library checks the point against Left/Right/Top/Bottom before calling the provider, since a point outside the element cannot identify text within it.","triggerScenarios":"Calling RangeFromPoint with screen coordinates from Cursor.Position or a mouse hook where the cursor has moved outside the element (e.g. off-window, over a title bar, or over another control) between capture and the call.","commonSituations":"Screen coordinates vs client coordinates confusion (passing client-relative points); cursor moved by user after point capture; DPI scaling or multi-monitor coordinates not translated to screen space; automation running while the window is dragged or minimized.","solutions":["Verify the point is inside the element's BoundingRectangle before calling RangeFromPoint.","Convert client/window coordinates to absolute screen coordinates (PointToScreen) before passing them.","Re-read BoundingRectangle immediately before the call to account for window movement, and clamp or reject points outside it."],"exampleFix":"// before\nvar range = textPattern.RangeFromPoint(clientPoint); // client-relative\n// after\nvar screenPoint = element.Cached.BoundingRectangle.Contains(clientPoint) ? clientPoint : window.PointToScreen(clientPoint);\nvar rect = element.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty) as Rect? ?? Rect.Empty;\nif (!rect.Contains(screenPoint)) return null;\nvar range = textPattern.RangeFromPoint(screenPoint);","handlingStrategy":"validation","validationCode":"var rect = (Rect)element.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty);\nbool inside = screenLocation.X >= rect.Left && screenLocation.X < rect.Right &&\n              screenLocation.Y >= rect.Top && screenLocation.Y < rect.Bottom;\nif (!inside) return null; // skip RangeFromPoint","typeGuard":"static bool IsWithinElement(AutomationElement el, Point p) =>\n    el.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty) is Rect r && r.Contains(p);","tryCatchPattern":"try { return textPattern.RangeFromPoint(screenLocation); }\ncatch (ArgumentException) { return null; // point outside element bounds }","preventionTips":["Always convert window/client coordinates to screen coordinates with PointToScreen","Capture Cursor.Position and use it immediately — the user may move the mouse","Re-read BoundingRectangle right before the call (windows move, DPI scales)","Account for multi-monitor negative coordinate spaces"],"tags":["uiautomation","textpattern","coordinates","argument-exception"],"backgroundTag":"value-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}