Reputation: 208
I am creating a console application that should call a VB application MyProj.exe and triggers a button click event on the same.
As of now I am able to run the executable file of the VB project but I wanted to fire the certain button click event from console application.
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = @"C:\New\MyProj.exe";
System.Diagnostics.Process.Start(startInfo);
i have tried with the below link - that is not working for me http://www.codeproject.com/Articles/14519/Using-Windows-APIs-from-C-again
-- the hwndChild after executing every statement is coming as "zero"
//Get a handle for the "5" button
hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","5");
//send BN_CLICKED message
SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);
//Get a handle for the "+" button
hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","*");
//send BN_CLICKED message
SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);
//Get a handle for the "2" button
hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","2");
//send BN_CLICKED message
SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);
//Get a handle for the "=" button
hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","=");
//send BN_CLICKED message
SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);
Thanks so much codecaster - but i would require little more help
#define AMP_PAUSE 40046
HWND hwnd = FindWindow("Winamp v1.x", 0);
if(hwnd) SendMessage(hwnd, WM_COMMAND, AMP_PAUSE, 0);
button1 is the id of the button; and Call_Method() is the procedure which is called when we click button1.
can you pls help how to write the above code in c#
Upvotes: 1
Views: 1150
Reputation: 112382
I'm suggesting you a slightly different approach. Add a shortcut key to your button. This is done by putting a &
before the letter you want to use as shortcut key in the button text. Then you can activate this button by entering Alt-X
, where X
is your shortcut key.
[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);
With this declaration you can then send the shortcut key to your application:
// Start your process
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\New\MyProj.exe";
Process process = Process.Start(startInfo);
// Wait for your process to be idle, sometimes an additional
// Thread.Sleep(...); is required for the application to be ready.
process.WaitForInputIdle();
// Make the started application the foreground window.
IntPtr h = process.MainWindowHandle;
SetForegroundWindow(h);
// Send it Alt-X
SendKeys.SendWait("%x");
Upvotes: 2