【问题标题】:Clicking a button in a Visual Basic application from .NET在 .NET 中单击 Visual Basic 应用程序中的按钮
【发布时间】: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

标签: c# vba c#-4.0 c#-3.0


【解决方案1】:

我建议您采用稍微不同的方法。为您的按钮添加快捷键。这是通过将& 放在要用作按钮文本中快捷键的字母之前完成的。然后您可以通过输入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");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 2014-02-10
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多