Getting a Button handle from another application

心已入冬 提交于 2021-01-27 04:12:10

问题


I have a program that needs to send the BM_CLICK message to another applications button. I can get the parent window handle but when I try to get the button handle if always returns 0

I got the button caption name and button type from Spy++ it seems right but I know I must have gotten something wrong. below is my code

 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);

    }

}

Spy screen shot

alt text


回答1:


Try to pass null for the window text and instead try to find any button:

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

After that you can use the second parameter and a new call to get the next button handle a couple more times.

Also could you please try checking Marshal.GetLastWin32Error to see what the error result is?




回答2:


Try this:

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



回答3:


You can do somthing like this:

//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);
        }
    }
}

This should do the trick.




回答4:


Try to build project as x86. I try and successed!



来源:https://stackoverflow.com/questions/3774738/getting-a-button-handle-from-another-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!