Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用UIAutomation在特定屏幕坐标下获取第三方应用程序中控件的窗口自动化id_C#_Ui Automation_Microsoft Ui Automation - Fatal编程技术网

C# 如何使用UIAutomation在特定屏幕坐标下获取第三方应用程序中控件的窗口自动化id

C# 如何使用UIAutomation在特定屏幕坐标下获取第三方应用程序中控件的窗口自动化id,c#,ui-automation,microsoft-ui-automation,C#,Ui Automation,Microsoft Ui Automation,我正在使用UIAutomation并尝试获取第三方应用程序中任何控件的窗口Id。我想知道如何在屏幕上的特定坐标处找到控件的ID 示例:我在桌面上运行计算器、记事本和Word。他们都在运行并部分共享屏幕。我希望能够运行我的程序,然后单击屏幕上的任何位置,并获取底层控件的窗口ID(如果鼠标下有) 我需要使用什么来实现此功能。我知道我需要某种鼠标挂钩,但真正的问题是如何在单击鼠标的屏幕上获取控件的窗口ID(而不是窗口句柄) 将在给定点返回自动化元素。一旦您有了它,您就可以轻松地获得自动化ID: pri

我正在使用UIAutomation并尝试获取第三方应用程序中任何控件的窗口Id。我想知道如何在屏幕上的特定坐标处找到控件的ID

示例:我在桌面上运行计算器、记事本和Word。他们都在运行并部分共享屏幕。我希望能够运行我的程序,然后单击屏幕上的任何位置,并获取底层控件的窗口ID(如果鼠标下有)

我需要使用什么来实现此功能。我知道我需要某种鼠标挂钩,但真正的问题是如何在单击鼠标的屏幕上获取控件的窗口ID(而不是窗口句柄)

将在给定点返回自动化元素。一旦您有了它,您就可以轻松地获得自动化ID:

private string ElementFromCursor()
{
    // Convert mouse position from System.Drawing.Point to System.Windows.Point.
    System.Windows.Point point = new System.Windows.Point(Cursor.Position.X, Cursor.Position.Y);
    AutomationElement element = AutomationElement.FromPoint(point);
    string autoIdString;
    object autoIdNoDefault = element.GetCurrentPropertyValue(AutomationElement.AutomationIdProperty, true);
    if (autoIdNoDefault == AutomationElement.NotSupported)
    {
           // TODO Handle the case where you do not wish to proceed using the default value.
    }
    else
    {
        autoIdString = autoIdNoDefault as string;
    }
    return autoIdString;
}

如果我理解正确,您的目标是-> 在屏幕的任意点单击,从运行的元素中获取基础元素(如果有)的窗口id:

如果是这种情况,以下内容应该有助于/给出一个想法(注意:这不仅会扩展光标位置,还会继续沿X轴搜索100个像素,间隔为10):

//
///获取基础元素的自动化标识符。
/// 
/// 
公共静态字符串GetTheAutomationIDOfUnderlyingElement()
{
string requiredAutomationID=string.Empty;
System.Drawing.Point currentLocation=Cursor.Position;//在此处添加当前位置
AutomationElement aeOfRequiredPaneAtTop=GetElementFromPoint(当前位置,10100);
如果(aeOfRequiredPaneAtTop!=null)
{
返回aeOfRequiredPaneAtTop.Current.AutomationId;
}
返回字符串。空;
}
/// 
///从点获取元素。
/// 
///起点。
///间隔时间。
///要遍历的最大长度。
/// 
公共静态自动元素GetElementFromPoint(点开始点、整数间隔、整数最大长度到行程)
{
AutomationElement requiredElement=null;
对于(点p=起始点;;)
{
requiredElement=AutomationElement.FromPoint(新的System.Windows.Point(p.X,p.Y));
Console.WriteLine(requireElement.Current.Name);
if(requireElement.Current.ControlType.Equals(ControlType.Window))
{
返回所需元素;
}
如果(p.X>(起始点X+最大长度至行程))
打破
p、 X=p.X+间隔;
}
返回null;
}
 /// <summary>
    /// Gets the automation identifier of underlying element.
    /// </summary>
    /// <returns></returns>
    public static string GetTheAutomationIDOfUnderlyingElement()
    {
        string requiredAutomationID = string.Empty;
        System.Drawing.Point currentLocation = Cursor.Position;//add you current location here
        AutomationElement aeOfRequiredPaneAtTop = GetElementFromPoint(currentLocation, 10, 100);
        if (aeOfRequiredPaneAtTop != null)
        {
            return aeOfRequiredPaneAtTop.Current.AutomationId;
        }
        return string.Empty;
    }
    /// <summary>
    /// Gets the element from point.
    /// </summary>
    /// <param name="startingPoint">The starting point.</param>
    /// <param name="interval">The interval.</param>
    /// <param name="maxLengthToTraverse">The maximum length to traverse.</param>
    /// <returns></returns>
    public static AutomationElement GetElementFromPoint(Point startingPoint, int interval, int maxLengthToTraverse)
    {
        AutomationElement requiredElement = null;
        for (Point p = startingPoint; ; )
        {
            requiredElement = AutomationElement.FromPoint(new System.Windows.Point(p.X, p.Y));
            Console.WriteLine(requiredElement.Current.Name);
            if (requiredElement.Current.ControlType.Equals(ControlType.Window))
            {
                return requiredElement;

            }
            if (p.X > (startingPoint.X + maxLengthToTraverse))
                break;
            p.X = p.X + interval;
        }
        return null;
    }