Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running exe inside WinForm Project Tab

I am interested in assistance in doing the following with a winform application I am writing to open in a tab an .exe in c# in the Visual Studios 2010 IDE.

I'm currently able to open the program via button click in the desired tab using the following code:

        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();

now how can i make it so this executable opens as the tab or in a tab, in my winform? I'm open to suggestions to doing either case.

SOLVED:

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);

    }
like image 251
PhoenixLament Avatar asked Sep 02 '25 16:09

PhoenixLament


1 Answers

You could use the SetParent api to set the parent of the executable's window. Add a panel to your TabControl and use the code below to assign the parent of the executable window to that of the panel.

[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);
}

To remove the window from the panel, use the same code but set the parent handle as IntPtr.Zero

SetParent(process.MainWindowHandle, IntPtr.Zero);
like image 81
Mo Patel Avatar answered Sep 04 '25 04:09

Mo Patel