2010-09-23 111 views
6

我有一個程序需要將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); 

    } 

} 

間諜屏幕截圖

alt text

回答

5

嘗試通過空的窗口文本,而是要找出什麼按鈕:

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

之後,你可以使用第二個參數和一個新的調用來使下一個按鈕處理幾次。

你也可以嘗試檢查Marshal.GetLastWin32Error看看錯誤的結果是什麼?

+0

嗨布賴恩,除非我誤解你的問題,我相信類名永遠必須是一個字符串否? – Mike 2010-09-23 01:23:01

+0

更正了我的答案。 – 2010-09-23 01:30:27

+0

嗨,布萊恩,好吧給一個嘗試,但仍然沒有:-) – Mike 2010-09-23 01:31:33

2

試試這個:

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

嘗試建立項目爲86。我嘗試併成功!

1

你可以做財產以後這樣的:

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

這應該做的伎倆。

相關問題