2014-11-03 68 views
1

我正在创建一个控制台应用程序,该应用程序应该调用VB应用程序MyProj.exe并触发同一按钮单击事件。单击.NET中的Visual Basic应用程序中的按钮

截至目前,我能够运行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

我检查了该实用程序 - 但我无法弄清楚如何调用按钮单击事件我vb.exe - 我也希望它在后台。 – 2014-11-03 11:31:30

+0

请显示您使用的实际代码。 “不为我工作”不够清楚,不能重新提出你的问题。使用SendMessage API是实现这一点的方法。 – CodeCaster 2014-11-03 13:40:26

+0

我已经下载了相同的代码,当我跑它 - 计算器打开,但与“0”的值。所以这意味着有按钮事件ared不会被解雇。每次hwndChild的值为“0” – 2014-11-03 14:55:48

回答

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"); 
相关问题