How to programmatically press a button at application launch using C # code

I want to write a program in C # that will run .exe (this is easy work, I know how to do it.), But now I want to add some more functions to it. I want to use user inputs for my program and want to pass them to run .exe as input. I also want to click on a specific button available in the exe window. The important thing is that I want to do this programmatically without user knowledge, that we are actually executing this exe in the background. Please let me know if possible. This is very urgent.

+3
source share
3 answers

Yes, this is possible with the Windows API. You need to send a message to this window. First use Spy ++ to get the identifiers of all the buttons that you want to click. Spy ++ is a Visual Studio tool and it shows which messages are sent to the application when these buttons are clicked. Then use the PostMessage () function (Windows API) to send the same message programmatically.

You can also see Winamp (music player for Windows), there is documentation on how to press buttons from an external application. You can do this the same way for other applications, you just need to know the identifiers of all the controls and the names of the windows or their classes.

here is the code for pressing the Winamp pause button:

#define AMP_PAUSE 40046
HWND hwnd = FindWindow("Winamp v1.x", 0);
if(hwnd) SendMessage(hwnd, WM_COMMAND, AMP_PAUSE, 0);

++. #, PInvoke Windows API. , , . FindWindow , . SendMessage PostMessage. : , .

Spy ++ , FindWindow SendMessage. , , , .

+6

WIN API, , , WindowScrape.

#. :

  • , .
  • (/ )
  • .

, , .

Edited

:

using System.Windows.Forms;

//WindowScrape
using WindowScrape.Types;

namespace WindowsFormsTestApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            var npad = HwndObject.GetWindowByTitle("Untitled - Notepad");
        }
    }
}
+1
+1

Source: https://habr.com/ru/post/1785716/


All Articles