編集

次の方法で共有


IRawElementProviderFragmentRoot.ElementProviderFromPoint Method

Definition

Retrieves the element in this fragment that is at the specified point.

public:
 System::Windows::Automation::Provider::IRawElementProviderFragment ^ ElementProviderFromPoint(double x, double y);
public System.Windows.Automation.Provider.IRawElementProviderFragment ElementProviderFromPoint (double x, double y);
abstract member ElementProviderFromPoint : double * double -> System.Windows.Automation.Provider.IRawElementProviderFragment
Public Function ElementProviderFromPoint (x As Double, y As Double) As IRawElementProviderFragment

Parameters

x
Double

The X coordinate.

y
Double

The Y coordinate.

Returns

The provider for the child element at the specified point, if one exists, or the root provider if the point is on this element but not on any child element. Otherwise returns null.

Examples

The following code example shows one possible implementation of this method for a non-scrolling list box. The index of the list item at the specified point is calculated using the height of each item, and the item at that point is returned. If no item exists at that point (for example, it is a blank area of the list box, the method returns a null reference (Nothing).

delegate Rectangle MyDelegate(Rectangle clientRect);

/// <summary>
/// Gets the child element that is at the specified point.
/// </summary>
/// <param name="x">Distance from the left of the application window.</param>
/// <param name="y">Distance from the top of the application window.</param>
/// <returns>The provider for the element at that point.</returns>
IRawElementProviderFragment IRawElementProviderFragmentRoot.ElementProviderFromPoint(
    double x, double y)
{
    // The RectangleToScreen method on the control can't be called directly from 
    // this thread, so use delegation.
    MyDelegate del = new MyDelegate(this.RectangleToScreen);
    Rectangle screenRectangle = (Rectangle)this.Invoke(del, new object[] { this.DisplayRectangle });

    if (screenRectangle.Contains((int)x, (int)y))
    {
        int index = (int)(((int)(y - screenRectangle.Y)) / itemHeight);
        if (index < myItems.Count)
        {
            return (IRawElementProviderFragment)myItems[index];
        }
        else
        {
            return (IRawElementProviderFragment)this;
        }
    }
    else
    {
        return null;
    }
}
Delegate Function MyDelegate(ByVal clientRect As Rectangle) As Rectangle

''' <summary>
''' Gets the child element that is at the specified point.
''' </summary>
''' <param name="x">Distance from the left of the application window.</param>
''' <param name="y">Distance from the top of the application window.</param>
''' <returns>The provider for the element at that point.</returns>
Function ElementProviderFromPoint(ByVal x As Double, ByVal y As Double) As IRawElementProviderFragment _
    Implements IRawElementProviderFragmentRoot.ElementProviderFromPoint

    Dim pointX As Integer = CInt(x)
    Dim pointY As Integer = CInt(y)

    ' The RectangleToScreen method on the control can't be called directly from 
    ' this thread, so use delegation.
    Dim converterDelegate As MyDelegate = New MyDelegate(AddressOf Me.RectangleToScreen)
    Dim screenRectangle As Rectangle = DirectCast(Me.Invoke(converterDelegate, _
        New Object() {Me.DisplayRectangle}), Rectangle)
    If screenRectangle.Contains(pointX, pointY) Then
        Dim index As Integer = CInt((pointY - screenRectangle.Y) \ itemHeight)
        If index < myItems.Count Then
            Return DirectCast(myItems(index), IRawElementProviderFragment)
        Else
            Return DirectCast(Me, IRawElementProviderFragment)
        End If
    Else
        Return Nothing
    End If
End Function

Remarks

If the point is on an element in another framework that is hosted by this fragment, the method returns the element that hosts that fragment.

The returned provider should correspond to the element that would receive mouse input at the specified point.

Applies to

See also