【发布时间】:2014-11-04 21:15:24
【问题描述】:
我正在创建一个控制台应用程序,它应该调用一个 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);
非常感谢编解码器 - 但我需要更多帮助
#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#写上面的代码吗
【问题讨论】:
-
我已经检查了该实用程序 - 但我无法弄清楚如何调用我的 vb.exe 的按钮单击事件 - 我也希望它在后台。
-
请显示您使用的实际代码。 “不为我工作”不够清楚,无法重新提出您的问题。使用 SendMessage API 就是这样做的方法。
-
我已经下载了相同的代码,当我运行它时 - 计算器打开但值为“0”。所以这意味着没有触发按钮事件。每次 hwndChild 值都为“0”
-
是的,但您仍然应该使用
FindWindow(Ex)和Send/PostMessage函数来伪造其他程序中的按钮点击。您的问题中没有足够的信息来解决您遇到的特定问题。 CodeProject 项目已有 8 年历史,也许它不再按预期工作,但它确实向您展示了它应该如何完成。另请参阅how to programmatically click on a button in running app using C# code。