tags:

views:

119

answers:

4

Hi,

I'm trying to launch another application from C# application, is there a way to display this application inside the mainform of my application?

Thanks,

+1  A: 

You can start other applications using Process.Start(...):

Process.Start(@"C:\Path\OtherApp.exe");

To embed the application within your form, check out this CodeProject article that demos a technique for hosting other application's windows within your form.

Simon P Stevens
He is asking about embedding another application into his.
Oded
@Oded: That's what the CodeProject article link is about. I'll edit to make that clearer.
Simon P Stevens
A: 

In general, it's next to impossible to display any kind of 3rd party application inside of yours. If target app supports console interface, I would create my own interface for this app that will translate GUI commands to console commands of target app.

Valera Kolupaev
+1  A: 

You can try do this via reparenting. See my post on MSDN where I describe this for WPF: Composite "shell" application.

The technique itself would be the same for WinForms. Have a host area in your app. Change the top-level window's style of the target application to WS_CHILD. Call SetParent(), changing the parent of the target window to your host area.

Note that in Win32, only a top-level window has a menu. Thus, changing to WS_CHILD removes the menu.

Daniel Rose
A: 

You can do it in that way :

    [DllImport("user32.dll")]
    static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    [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 void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

    [DllImport("user32.dll")]
    static extern IntPtr SetActiveWindow(IntPtr hWnd);


    private const int GWL_STYLE = (-16);
    private const int WS_VISIBLE = 0x10000000;
    private const int WS_MAXIMIZE = 0x01000000;

    private void Form1_Load(object sender, EventArgs e)
    {
        this.SuspendLayout();

        Process notepad = new Process();
        ProcessStartInfo psi = new ProcessStartInfo("notepad.exe");
        psi.WindowStyle = ProcessWindowStyle.Normal;
        notepad.StartInfo = psi;

        notepad.Start();

        this.ResumeLayout();

        notepad.WaitForInputIdle(3000);

        IntPtr old = SetParent(notepad.MainWindowHandle, this.Handle);

        SetWindowLong(notepad.MainWindowHandle, GWL_STYLE, WS_VISIBLE + WS_MAXIMIZE);
        MoveWindow(notepad.MainWindowHandle, 100, 100, 400, 400, true);

        SetActiveWindow(notepad.MainWindowHandle);
        SwitchToThisWindow(notepad.MainWindowHandle, true);  }

In this way you have Notepad app in your form ;)