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

前端 未结 12 1396
小蘑菇
小蘑菇 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: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)

提交回复
热议问题