dotnet/wpf · error · ArgumentException
SR.ScreenCoordinatesOutsideBoundingRect
Error message
SR.ScreenCoordinatesOutsideBoundingRect
What it means
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.
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.
Example fix
// before var range = textPattern.RangeFromPoint(clientPoint); // client-relative // after var screenPoint = element.Cached.BoundingRectangle.Contains(clientPoint) ? clientPoint : window.PointToScreen(clientPoint); var rect = element.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty) as Rect? ?? Rect.Empty; if (!rect.Contains(screenPoint)) return null; var range = textPattern.RangeFromPoint(screenPoint);
Defensive patterns
Strategy: validation
Validate before calling
var rect = (Rect)element.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty);
bool inside = screenLocation.X >= rect.Left && screenLocation.X < rect.Right &&
screenLocation.Y >= rect.Top && screenLocation.Y < rect.Bottom;
if (!inside) return null; // skip RangeFromPoint Type guard
static bool IsWithinElement(AutomationElement el, Point p) =>
el.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty) is Rect r && r.Contains(p); Try / catch
try { return textPattern.RangeFromPoint(screenLocation); }
catch (ArgumentException) { return null; // point outside element bounds } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.
Related errors
- SR.Format(SR.TextAttributeValueWrongType, attribute…
- SR.UnsupportedAttribute
- Character offset not valid in TextRange.
- E_INVALIDARG
- name
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/1b06ee4aed61fab7.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/TextPattern.cs:220
return TextPatternRange.Wrap(hTextRange, this);
}
/// <summary>
/// Finds the range nearest to a screen coordinate.
/// If the coordinate is within the bounding rectangle of a character then the
/// range will contain that character. Otherwise, it will be a degenerate
/// range near the point, chosen in an implementation-dependent manner.
/// An InvalidOperation exception is thrown if the point is outside of the
/// client area of the text container.
/// </summary>
/// <param name="screenLocation">The location in screen coordinates.</param>
/// <returns>A degenerate range nearest the specified location.</returns>
public TextPatternRange RangeFromPoint(Point screenLocation)
{
//If we are not within the client area throw an exception
Rect rect = (Rect)_element.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty);
if (screenLocation.X < rect.Left || screenLocation.X >= rect.Right || screenLocation.Y < rect.Top || screenLocation.Y >= rect.Bottom)
{
throw new ArgumentException(SR.ScreenCoordinatesOutsideBoundingRect);
}
SafeTextRangeHandle hTextRange = UiaCoreApi.TextPattern_RangeFromPoint(_hPattern, screenLocation);
return TextPatternRange.Wrap(hTextRange, this);
}
#endregion Public Methods
//------------------------------------------------------
//
// Public Properties
//
//------------------------------------------------------
#region Public Properties
/// <summary>
/// A text range that encloses the main text of the document. Some auxillary text such as View on GitHub (pinned to 81131a70a4)