我正在创建一个控制台应用程序,该应用程序应该调用VB应用程序MyProj.exe并在其上触发按钮单击事件。
截至目前,我可以运行VB项目的可执行文件,但我想从控制台应用程序中触发某个按钮单击事件。
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = @"C:\New\MyProj.exe";
System.Diagnostics.Process.Start(startInfo);
我尝试过以下链接 - 这对我不起作用 http://www.codeproject.com/Articles/14519/Using-Windows-APIs-from-C-again
- 执行每个语句后的hwndChild将变为“零”
//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);
非常感谢codecaster - 但我需要更多的帮助
#define AMP_PAUSE 40046
HWND hwnd = FindWindow("Winamp v1.x", 0);
if(hwnd) SendMessage(hwnd, WM_COMMAND, AMP_PAUSE, 0);
button1是按钮的id;和Call_Method()是我们点击button1时调用的过程。
请问如何在c#
中编写上述代码答案 0 :(得分:2)
我建议你采用略有不同的方法。在按钮上添加快捷键。这是通过在要用作按钮文本中的快捷键的字母前面放置一个&
来完成的。然后,您可以输入Alt-X
来激活此按钮,其中X
是您的快捷键。
[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);
使用此声明,您可以将快捷键发送到您的应用程序:
// 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");