2010-09-23 34 views
6

Tôi có một chương trình cần gửi tin nhắn BM_CLICK đến một nút ứng dụng khác. Tôi có thể lấy nút điều khiển cửa sổ chính nhưng khi tôi cố gắng xử lý nút nếu luôn trả về 0Nhận một nút xử lý từ một ứng dụng khác

Tôi nhận được tên và nút chú thích nút từ Spy ++, có vẻ đúng nhưng tôi biết tôi đã nhận được lỗi. dưới đây là mã của tôi

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 màn bắn

alt text

Trả lời

5

Cố gắng vượt qua null cho các văn bản cửa sổ và thay vào đó cố gắng để tìm thấy bất kỳ nút:

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

Sau đó bạn có thể sử dụng tham số thứ hai và một cuộc gọi mới để nút tiếp theo xử lý một vài lần nữa.

Bạn cũng có thể thử kiểm tra Marshal.GetLastWin32Error để xem kết quả lỗi là gì?

+0

hi Brian, Trừ khi tôi hiểu lầm những gì bạn hỏi tôi tin rằng tên lớp luôn luôn phải là một chuỗi không? – Mike

+0

Đã sửa câu trả lời của tôi. –

+0

Xin chào Brian, Ok hãy thử và vẫn không có gì :-) – Mike

2

Hãy thử điều này:

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

Cố gắng xây dựng dự án như x86. Tôi cố gắng và thành công!

1

Bạn có thể làm somthing như thế này:

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

này nên làm các trick.

Các vấn đề liên quan