/// <summary>
/// Returns the degenerate (empty) text range nearest to the specified screen coordinates.
/// </summary>
/// <param name="screenLocation">The location in screen coordinates.</param>
/// <returns>A degenerate range nearest the specified location. Null is never returned.</returns>
public override UiaCore.ITextRangeProvider?RangeFromPoint(Point screenLocation)
{
if (!_owningTextBoxBase.IsHandleCreated)
{
return(null);
}
Point clientLocation = screenLocation;
// Convert screen to client coordinates.
// (Essentially ScreenToClient but MapWindowPoints accounts for window mirroring using WS_EX_LAYOUTRTL.)
if (MapWindowPoint(IntPtr.Zero, _owningTextBoxBase, ref clientLocation) == 0)
{
return(new UiaTextRange(_owningTextBoxBase.AccessibilityObject, this, start: 0, end: 0));
}
// We have to deal with the possibility that the coordinate is inside the window rect
// but outside the client rect. In that case we just scoot it over so it is at the nearest
// point in the client rect.
RECT clientRectangle = _owningTextBoxBase.ClientRectangle;
clientLocation.X = Math.Max(clientLocation.X, clientRectangle.left);
clientLocation.X = Math.Min(clientLocation.X, clientRectangle.right);
clientLocation.Y = Math.Max(clientLocation.Y, clientRectangle.top);
clientLocation.Y = Math.Min(clientLocation.Y, clientRectangle.bottom);
// Get the character at those client coordinates.
int start = _owningTextBoxBase.GetCharIndexFromPosition(clientLocation);
return(new UiaTextRange(_owningTextBoxBase.AccessibilityObject, this, start, start));
}