在WinForm项目选项卡中运行exe

本文关键字:运行 exe 选项 WinForm 项目 | 更新日期: 2023-09-27 18:03:58

我有兴趣帮助我在Visual studio 2010 IDE中编写一个winform应用程序,在c#中以选项卡和。exe打开以下内容。

我目前能够打开程序通过按钮点击所需的选项卡使用以下代码:

        string str = @"-INSERT FILEPATH HERE-";//took file path out as i have a few exes i'm wanting to add. 
        Process process = new Process();
        process.StartInfo.FileName = str;
        process.Start();

现在我怎样才能使这个可执行文件在我的winform中以TAB或TAB打开?两种情况我都愿意接受建议。

解决:

using System.Runtime.InteropServices;
    using System.Threading;
    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);
    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
   public GUI()
    {
        // Initialize form fieds
        InitializeComponent();
        openProgram()
    }
    private void openProgram()
    {
        process.StartInfo.FileName = "-filepathhere-";
        process.Start();
        IntPtr ptr = IntPtr.Zero;
        while ((ptr = process.MainWindowHandle) == IntPtr.Zero) ;
        SetParent(process.MainWindowHandle, trackerPanel.Handle);
        MoveWindow(process.MainWindowHandle, 0, 0, this.Width - 90, this.Height, true);
    }

在WinForm项目选项卡中运行exe

你可以使用SetParent api来设置可执行窗口的父窗口。在TabControl中添加一个面板,并使用下面的代码将可执行窗口的父窗口分配给该面板的父窗口。

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
private void button2_Click(object sender, EventArgs e)
{
    var process = new Process();
    process.StartInfo.FileName = "notepad.exe";
    process.Start();
    SetParent(process.MainWindowHandle, panel1.Handle);
}

要从面板中删除窗口,使用相同的代码,但将父句柄设置为IntPtr。0

SetParent(process.MainWindowHandle, IntPtr.Zero);