jake2006 2023-08-28 08:18 采纳率: 0%
浏览 6

c#的Winform 参数传递

有人知道在c#的Winform程序中怎么调用另一个exe文件,并传递参数以及模拟被调用的EXE程序的按钮事件?

  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2023-08-28 08:48
    关注

    引用 皆我百晓生 小程序回复内容作答:

    在C#的Winform程序中,你可以使用Process类来调用另一个exe文件,并传递参数。同时,模拟被调用exe程序的按钮事件可以通过发送消息或其他机制来实现。下面是一个示例代码:

    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace WinformApp
    {
        public partial class MainForm : Form
        {
            [DllImport("user32.dll", SetLastError = true)]
            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
            [DllImport("user32.dll")]
            private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    
            private const int BM_CLICK = 0x00F5;
    
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void callExeButton_Click(object sender, EventArgs e)
            {
                string exePath = "path/to/another.exe";
                string parameter = "your_parameter";
    
                // 启动另一个exe程序并传递参数
                Process.Start(exePath, parameter);
            }
    
            private void simulateButtonClickButton_Click(object sender, EventArgs e)
            {
                string exeWindowTitle = "Another EXE Window Title";
                string buttonClassName = "Button";
                string buttonText = "Button Text";
    
                // 查找另一个exe程序窗口
                IntPtr windowHandle = FindWindow(null, exeWindowTitle);
    
                // 发送按钮点击消息给另一个exe程序窗口
                SendMessage(windowHandle, BM_CLICK, 0, 0);
            }
        }
    }
    

    在以上示例代码中,callExeButton_Click方法演示了如何调用另一个exe程序并传递参数,你需要将exePath设置为目标exe文件的路径,并将parameter设置为要传递的参数。

    simulateButtonClickButton_Click方法演示了如何模拟另一个exe程序的按钮点击事件,你需要将exeWindowTitle设置为目标exe程序的窗口标题,buttonClassName设置为按钮的类名,buttonText设置为按钮的文本。

    请注意,以上代码仅提供了一个基本示例,具体的实现可能需要根据你实际的需求进行修改和调整。同时,模拟按钮点击事件的方式可能因被调用的exe程序的实现方式不同而有所差异,你需要根据被调用的exe程序的具体情况进行相应的处理和适配。

    评论

报告相同问题?

问题事件

  • 创建了问题 8月28日