4

C# で外部アプリケーションから UI テキストを取得することは可能ですか?

特に、サード パーティによって作成された外部の Win32 アプリから、ラベル (通常の Windows ラベル コントロールだと思います) から Unicode テキストを読み取る方法はありますか? テキストは表示されますが、UI でマウスで選択することはできません。

これを可能にするいくつかのアクセシビリティ API (スクリーン リーダー用など) があると思います。

編集: 現在、Managed Spy Appのようなものを使用することを検討していますが、他のリードを歓迎します.

4

3 に答える 3

6

標準の Win32 ラベルだけを気にする場合は、他の回答で概説されているように、WM_GETTEXTが正常に機能します。

--

標準ラベル用のアクセシビリティ API - UIAutomation - があり、これもバックグラウンドで WM_GETTEXT を使用します。ただし、これの利点の 1 つは、ほとんどのシステム コントロールを含む他のいくつかのタイプのコントロールからテキストを取得できることです。多くの場合、WPF、IE や Firefox のテキストなど、システム以外のコントロールを使用する UI も取得できます。

// compile as:
// csc file.cs /r:UIAutomationClient.dll /r:UIAutomationTypes.dll /r:WindowsBase.dll
using System.Windows.Automation;
using System.Windows.Forms;
using System;

class Test
{
    public static void Main()
    {
        // Get element under pointer. You can also get an AutomationElement from a
            // HWND handle, or by navigating the UI tree.
        System.Drawing.Point pt = Cursor.Position;
        AutomationElement el = AutomationElement.FromPoint(new System.Windows.Point(pt.X, pt.Y));
        // Prints its name - often the context, but would be corresponding label text for editable controls. Can also get the type of control, location, and other properties.
        Console.WriteLine( el.Current.Name );
    }
}
于 2012-06-14T22:34:33.400 に答える
5

その Unicode テキストが実際にキャプション付きのウィンドウである場合は、WM_GETTEXTメッセージを送信することで実行できます。

[DllImport("user32.dll")]
public static extern int SendMessage (IntPtr hWnd, int msg, int Param, System.Text.StringBuilder text);

System.Text.StringBuilder text = new System.Text.StringBuilder(255) ;  // or length from call with GETTEXTLENGTH
int RetVal = Win32.SendMessage( hWnd , WM_GETTEXT, text.Capacity, text);

キャンバスに描かれただけの場合、アプリケーションが使用するフレームワークを知っていれば、うまくいくかもしれません。WinForms または Borland の VCL を使用している場合は、その知識を使用してテキストにアクセスできます。

于 2008-08-20T20:04:35.967 に答える
2

その記事には wm_gettext または wm_gettextlength の値が表示されていないので、念のため..

const int WM_GETTEXT = 0x0D;
const int WM_GETTEXTLENGTH = 0x0E;
于 2008-08-20T21:08:33.737 に答える