Programmatically press a button on another application (C, Windows)

前端 未结 12 1379
小蘑菇
小蘑菇 2021-01-30 14:47

I\'m trying to use the following code to press a button on my other application:

HWND ButtonHandle;
if( (wnd = FindWindow(0, \"Do you want to save?\")) )
{   
           


        
相关标签:
12条回答
  • 2021-01-30 15:36

    maybe this can help: http://www.cplusplus.com/forum/beginner/8806/

    0 讨论(0)
  • 2021-01-30 15:37

    //Send digit 4 to the already opened calc.exe

    HWND windowHandle;
    
    windowHandle = FindWindowA(NULL,"Calculator");
    
    if(windowHandle != 0)
       ret = EnumChildWindows(windowHandle,GetButtonHandle,0);
    
    BOOL CALLBACK GetButtonHandle(HWND handle, LPARAM)
    {
    char label[100];
    int size = GetWindowTextA(handle,label,sizeof(label));
    
    if(strcmp(label,"4") == 0)
    {
    PostMessage(handle ,WM_LBUTTONDOWN,(WPARAM)0x0001,0);
    PostMessage(handle ,WM_LBUTTONUP,(WPARAM)0x0001,0);
    
    PostMessage(handle ,WM_LBUTTONDOWN,(WPARAM)0x0001,0);
    PostMessage(handle ,WM_LBUTTONUP,(WPARAM)0x0001,0);
    
    return false;
    }
    return true;
    }
    
    0 讨论(0)
  • 2021-01-30 15:39
    SendMessage(btnHandle, WM_LBUTTONDOWN, 0, 0);
    SendMessage(btnHandle, WM_LBUTTONUP, 0, 0);
    SendMessage(btnHandle, WM_LBUTTONDOWN, 0, 0);
    SendMessage(btnHandle, WM_LBUTTONUP, 0, 0);
    

    You have to send a button click twice. Not sure why (maybe the first click only activates the window of the button), but I'm using this code for a long time and it always worked for me.

    0 讨论(0)
  • 2021-01-30 15:39

    See the following solution, also you can use

    SendMessage(ButtonHandle, WM_LBUTTONDOWN, 0, 0);
    SendMessage(ButtonHandle, WM_LBUTTONUP, 0, 0);
    

    Or

    SendMessage(ButtonHandle, BM_CLICK, 0, 0);
    
    HWND buttonHandle = 0;
    
    BOOL CALLBACK GetButtonHandle(HWND handle, LPARAM)
    {
     char label[100];
     int size = GetWindowTextA(handle,label,sizeof(label));
     if(strcmp(label,"&Save") == 0)
     {
      buttonHandle = handle;
      return false;
     }
     return true;
    }
    void main()
    {
     HWND windowHandle = FindWindowA(NULL,"Do you want to Save?");
     if(windowHandle != 0)
     {
      BOOL ret = EnumChildWindows(windowHandle,GetButtonHandle,0);
    
      if(buttonHandle != 0)
      {
       LRESULT res = SendMessage(buttonHandle,BM_CLICK,0,0);
       //SendMessage(buttonHandle,WM_LBUTTONDOWN,0,0); 
       //SendMessage(buttonHandle,WM_LBUTTONUP,0,0);
      }
    
     }
    
    
    
    }
    

    Note: Getting sure from the window text,button text (check if there is space at the end of the window title)

    0 讨论(0)
  • 2021-01-30 15:44
    1. Are you sure that "SaveButton" class name is valid? Do you get the button handle?
    2. Try to send messages to ButtonHandle window (directly to the button).

    Update: I believe this should work,

    SendMessage(ButtonHandle, BM_CLICK, 0, 0);
    
    0 讨论(0)
  • 2021-01-30 15:46

    Microsoft is now pushing Active Accessibility (MSAA) for UI Automation, (It has been renamed a number of times over the years) see

    • UI Automation and Microsoft Active Accessibility
    • Using UI Automation for Automated Testing
    • UI Automation Clients for Managed Code

    Sorry I don’t have any simple code to get you started. As “SendMessage()” does not seem to be working for you, I don’t know of another option apart from “UI Automation”

    I am assuming you have check with Spy++ (installed with MsDev) that you message are being send to the correct button etc – and that the button is a standard windows buttons. My first instant would say use “SendMessage()" or "PostMessage()” but given the numbers of answers about “SendMessage()” and the fact it is not working for you. I expect someone is going on…

    0 讨论(0)
提交回复
热议问题