2010-09-23 11 views
6

Ho un programma che deve inviare il messaggio BM_CLICK a un altro pulsante di applicazioni. Posso ottenere l'handle della finestra genitore ma quando cerco di ottenere l'handle del pulsante se restituisce sempre 0Ottenere un handle Button da un'altra applicazione

Ho il nome del pulsante e il tipo di pulsante da Spy ++ sembra giusto, ma so che devo aver trovato qualcosa di sbagliato. qui di seguito è il mio codice

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

    } 

} 

schermo Spy girato

alt text

risposta

5

Provate a passare null per il testo finestra e invece cercare di trovare un qualsiasi tasto:

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

Dopo di che si può usa il secondo parametro e una nuova chiamata per ottenere il pulsante successivo per un altro paio di volte.

Inoltre, potresti provare a verificare Marshal.GetLastWin32Error per vedere quale è il risultato dell'errore?

+0

ciao Brian, a meno che non fraintenda ciò che stai chiedendo, credo che il nome della classe debba sempre essere una stringa no? – Mike

+0

Correggere la mia risposta. –

+0

Ciao Brian, Ok, provalo e ancora niente :-) – Mike

2

Prova questo:

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

cercare di costruire progetto come x86. Ci provo e successo!

1

Si può fare somthing come questo:

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

Questo dovrebbe fare il trucco.

Problemi correlati