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

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

    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.

提交回复
热议问题