2

プラグインなどを使用できない外部プログラムの一部の機能を拡張しようとしています。したがって、この外部アプリケーションのテキストボックスからテキストを読み取り、いくつかの独自のアクションをトリガーする独自のアプリを作成する必要があります。

user32.dllのFindWindowAPIを使用することで、外部アプリケーションのハンドルをすでに把握しています。しかし今、私はちょっと立ち往生しています。Visual StudioToolsのSpy++を使用することで、読み取りたいコントロールのクラス名が「WindowsForms10.EDIT.app.0.218f99c」であるという情報を取得しましたが、いくつかあります。さらに、外部アプリが起動するたびに、読み取りたいテキストボックスの新しいコントロールIDが作成されます。

特定のテキストボックスを識別してその値を読み取ることができるようにするにはどうすればよいですか?誰かが私にヒントやアドバイスを教えてもらえますか?

4

2 に答える 2

4

C#のコード

public static class ModApi
{
    [DllImport("user32.dll", EntryPoint = "FindWindowA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

    [DllImport("user32.dll", EntryPoint = "SendMessageTimeout", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern uint SendMessageTimeoutText(IntPtr hWnd, int Msg, int countOfChars, StringBuilder text, uint flags, uint uTImeoutj, uint result);

    [DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    static internal extern bool EnumChildWindows(IntPtr hWndParent, funcCallBackChild funcCallBack, IntPtr lParam);

    public delegate bool funcCallBackChild(IntPtr hWnd, IntPtr lParam);


    public static ArrayList TextBoxStrings = new ArrayList();
    public static void ParseWindowControls(string WindowTitle)
    {
        IntPtr hWnd = FindWindow(null, WindowTitle);
        TextBoxStrings.Clear();


        funcCallBackChild MyCallBack = EnumChildWindowsProc;
        EnumChildWindows(hWnd, MyCallBack, IntPtr.Zero);
    }

    private static bool EnumChildWindowsProc(IntPtr hWndParent, IntPtr lParam)
    {
        var buffer = new StringBuilder(256);
        long Retval = GetClassName(hWndParent, buffer, buffer.Capacity);
        if (buffer.ToString() == "Edit" || buffer.ToString() == "Static")
        {
            TextBoxStrings.Add(GetText(hWndParent));
        }

        return true;
    }

    private static string GetText(IntPtr hwnd)
    {
        var text = new StringBuilder(1024);
        if (SendMessageTimeoutText(hwnd, 0xd, 1024, text, 0x2, 1000, 0) != 0)
        {
            return text.ToString();
        }

        return "";
    }
}
于 2012-01-04T15:51:07.750 に答える
3

いくつかのページを偶然見つけた後、外部アプリケーションのテキストボックスからコンテンツを取得する方法を見つけました。このコードは他の人にも役立つと思うので、結果は次のとおりです。

Module modApi

    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hwnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Private Declare Function SendMessageTimeoutString Lib "user32.dll" Alias "SendMessageTimeoutA" (ByVal hwnd As IntPtr, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As String, ByVal fuFlags As Long, ByVal uTimeout As Long, ByVal lpdwResult As Long) As Long
    Friend Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As IntPtr, ByVal funcCallBack As funcCallBackChild, ByVal lParam As IntPtr) As Boolean

    Public Delegate Function funcCallBackChild(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean

    Public TextBoxStrings As ArrayList = New ArrayList

    Public Sub ParseWindowControls(ByVal WindowTitle As String)
        Dim hWnd As IntPtr = FindWindow(vbNullString, WindowTitle)
        TextBoxStrings.Clear()
        If hWnd Then
            Dim MyCallBack As New funcCallBackChild(AddressOf EnumChildWindowsProc)
            EnumChildWindows(hWnd, MyCallBack, IntPtr.Zero)
        Else
            MsgBox("Could not find window!", vbOKOnly + vbExclamation, "Error")
        End If
    End Sub

    Private Function EnumChildWindowsProc(ByVal hWndParent As IntPtr, ByVal lParam As IntPtr) As Boolean
        Dim Buffer As String = Space(256)
        Dim Retval As Long = GetClassName(hWndParent, Buffer, Len(Buffer))
        If Left(Buffer, Retval) = "WindowsForms10.EDIT.app.0.218f99c" Then
            TextBoxStrings.Add(GetText(hWndParent))
        End If
        EnumChildWindowsProc = True
    End Function

    Private Function GetText(ByVal hwnd As IntPtr) As String
        Dim sText As String = Space(1024)
        If SendMessageTimeoutString(hwnd, &HD, 1024, sText, &H2, 1000, 0) <> 0 Then
            GetText = Left(sText, InStr(sText, vbNullChar) - 1)
            Exit Function
        End If
        GetText = ""
    End Function

End Module

サブにウィンドウタイトルを渡すことができますParseWindowControls()。潜水艦は要求されたウィンドウを見つけようとします。ウィンドウが見つかると、このアプリケーションで見つかったすべてのコントロールの収集が開始されます。コールバックは、見つかったコントロールが私の仕様(テキストボックス)を満たしているかどうかを調べ、テキストを配列リストに格納します。後で、リクエセットテキストボックスのインデックスを知って、それを配列リストから取得する必要があります。それでおしまい。これが他の人にも役立つことを願っています。

于 2011-09-27T14:38:02.527 に答える