1

私は UIAutomation を使用しており、サード パーティ製アプリケーションの任意のコントロールのウィンドウ ID を取得しようとしています。画面上の特定の座標にあるコントロールの ID を調べる方法を知りたいです。

例: デスクトップで電卓、メモ帳、および Word を実行しています。それらのすべてが実行され、画面を部分的に共有しています。プログラムを実行し、画面上の任意の場所をクリックして、基になるコントロールのウィンドウ ID を取得できるようにしたい (マウスの下にある場合)。

この機能を実現するには、何を使用する必要がありますか。ある種のマウス フックが必要であることは理解していますが、実際の問題は、マウスがクリックされた画面上のコントロールのウィンドウ ID (ウィンドウ ハンドルではない) を取得する方法です。

4

2 に答える 2

1

私がそれを正しく理解している場合、あなたが達成しようとしているのは->画面の任意のポイントをクリックして、実行中の要素から、基になる要素のウィンドウIDを取得します。

その場合は、次の方法でアイデアを得ることができます (注: これはカーソル位置だけでなく、X 軸に沿って 10 の間隔で 100 ピクセルを検索し続けます):

 /// <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;
    }
于 2014-11-18T15:46:55.540 に答える
1

AutomationElement.FromPoint()指定された時点でオートメーション要素を返します。それができたら、自動化 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;
}
于 2014-03-20T22:54:51.040 に答える