7

BM_CLICKメッセージを別のアプリケーションボタンに送信する必要があるプログラムがあります。親ウィンドウのハンドルを取得できますが、ボタンハンドルを取得しようとすると、常に0が返されます。

Spy ++からボタンのキャプション名とボタンの種類を取得しましたが、それは正しいようですが、何か問題があったに違いありません。以下は私のコードです

 public const Int BM_CLICK = 0x00F5;

 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SendMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);



private void button1_Click(object sender, EventArgs e)
{
    Process[] processes = Process.GetProcessesByName("QSXer");

    foreach (Process p in processes)
    {
        ////the Button's Caption is "Send" and it is a "Button".  
        IntPtr ButtonHandle = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, "Button", "Send");
       //ButtonHandle is always zero thats where I think the problem is 
    SendMessage(ButtonHandle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);

    }

}

スパイのスクリーンショット

代替テキスト

4

4 に答える 4

5

ウィンドウ テキストに null を渡して、代わりに任意のボタンを検索してみてください。

IntPtr ButtonHandle = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, "Button", null);

その後、2 番目のパラメーターと新しい呼び出しを使用して、次のボタン ハンドルをさらに数回取得できます。

またMarshal.GetLastWin32Error、エラー結果がどうなるか確認してみていただけますでしょうか。

于 2010-09-23T01:17:31.180 に答える
2

これを試して:

IntPtr ButtonHandle = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, null, "Send");
于 2012-12-25T07:48:35.127 に答える
2

次のようなことができます。

//Program finds a window and looks for button in window and clicks it

HWND buttonHandle = 0;

BOOL CALLBACK GetButtonHandle(HWND handle, LPARAM)
{
    char label[100];
    int size = GetWindowTextA(handle, label, sizeof(label));
    if (strcmp(label, "Send") == 0) // your button name
    {
        buttonHandle = handle;
        cout << "button id is: " << handle << endl;
        return false;
    }
    return true;
}

int main()

{
    HWND windowHandle = FindWindowA(NULL, "**Your Window Name**");

    if (windowHandle == NULL)
    {
        cout << "app isn't open." << endl;
    }

    else
    {
        cout << "app is open :) " << endl;
        cout << "ID is: " << windowHandle << endl;
        SetForegroundWindow(windowHandle);
        BOOL ret = EnumChildWindows(windowHandle, GetButtonHandle, 0); //find the button
        cout << buttonHandle << endl;
        if (buttonHandle != 0)
        {
            LRESULT res = SendMessage(buttonHandle, BM_CLICK, 0, 0);
        }
    }
}

これでうまくいくはずです。

于 2016-12-14T10:04:15.743 に答える
0

プロジェクトを x86 としてビルドしてみてください。私は試して成功しました!

于 2014-11-21T03:36:13.760 に答える