Host external app in a WPF panel
<p>I am trying to host/run an Exe application within a panel inside of the main window of my program. The main window has a grid with 3 columns - two columns split evenly with a vertical grid splitter to section the left and right columns. The left column has a tab control, but the right column is what I want to place the exe application inside of and essentially have the application resize based on the dock inside of the right column.</p>

<p>I have built the program in a similar fashion to <a href="https://stackoverflow.com/questions/5028598/hosting-external-app-in-wpf-window">Hosting external app in WPF window</a>. This example is much simpler than what I"m going to use for the exe and additional functionality but the example should explain my current issue. </p>

<p>I can launch the exe into the window, but not within the Dockpanel, or any other control, in the right column. The exe just stays inside of the window statically where it's placed as opposed to being placed inside of the column. I can go in and change the starting location of the process window so it actually opens on top of the right column, and appears to be within that column's dock panel but again it's only static. </p>

<p>All of the examples I've seen are placing a process inside of the window, and not within a container. I've tried setting the WindowsInteropHelper to the dock panel but it still just sends it to the main window. </p>

<pre><code>var helper = new WindowInteropHelper(GetWindow(this.ApplicationDock));
</code></pre>

<p>I've also tried a handful of articles including the two below but still haven't found the solution I"m looking for. </p>

<p><a href="https://stackoverflow.com/questions/758494/how-can-i-run-another-application-within-a-panel-of-my-c-sharp-program">How can I run another application within a panel of my C# program?</a></p>

<p><a href="https://stackoverflow.com/questions/5836176/docking-window-inside-another-window">Docking Window inside another Window</a></p>

<p>One possibility I've thought about, is upon window change update the process window handler to be placed at the correct coordinates of where the right column starts at. To me this seems like it should be the last resort though and not the correct way to handle this.</p>

<p>My main application</p>

<pre><code>&lt;Window x:Class="ProgramInProgram.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ProgramInProgram"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525"&gt;
&lt;Grid&gt;
    &lt;Grid.ColumnDefinitions&gt;
        &lt;ColumnDefinition /&gt;
        &lt;ColumnDefinition Width="Auto"/&gt;
        &lt;ColumnDefinition /&gt;
    &lt;/Grid.ColumnDefinitions&gt;
    &lt;TabControl Grid.Column="0"&gt;
        &lt;TabItem Header="Blue Tab"&gt;
            &lt;Grid Background="Blue"&gt;&lt;/Grid&gt;
        &lt;/TabItem&gt;
        &lt;TabItem Header="Red Tab" &gt;
            &lt;Grid Background="Red"&gt;&lt;/Grid&gt;
        &lt;/TabItem&gt;
    &lt;/TabControl&gt;
    &lt;GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch" /&gt;
    &lt;DockPanel Grid.Column="2" x:Name="ApplicationDock"&gt;
    &lt;/DockPanel&gt;
&lt;/Grid&gt;
&lt;/Window&gt;
</code></pre>

<p>The Code behind</p>

<pre><code>public partial class MainWindow : Window
{

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

    [DllImport("user32.dll", SetLastError = true)]
    public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

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

    [DllImport("user32.dll")]
    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, int uFlags);

    private Process process;

    private const int SWP_ZOZORDER = 0x0004;
    private const int SWP_NOACTIVATE = 0x0010;
    private const int GWL_STYLE = (-16);
    private const int WS_CAPTION = 0x00C00000;
    private const int WS_VISIBLE = 0x10000000;
    private const int WS_THICKFRAME = 0x00040000;
    const string patran = "patran";

    public MainWindow()
    {
        InitializeComponent();

        Loaded += (s, e) =&gt; LaunchChildProcess();
    }

    private void LaunchChildProcess()
    {
        this.process = Process.Start("Notepad.exe");
        this.process.WaitForInputIdle();

        var helper = new WindowInteropHelper(GetWindow(this.ApplicationDock));

        SetParent(this.process.MainWindowHandle, helper.Handle);

        int style = GetWindowLong(this.process.MainWindowHandle, GWL_STYLE);
        style = style &amp; ~WS_CAPTION &amp; ~WS_THICKFRAME;
        SetWindowLong(this.process.MainWindowHandle, GWL_STYLE, style);
        ResizeEmbeddedApp();
    }

    private void ResizeEmbeddedApp()
    {
        if (this.process == null)
        {
            return;
        }

        UIElement container = VisualTreeHelper.GetParent(this.ApplicationDock) as UIElement;
        Point relativeLocation = this.ApplicationDock.TranslatePoint(new Point(0, 0), container);
        }

    protected override Size MeasureOverride(Size availableSize)
    {
        Size size = base.MeasureOverride(availableSize);
        ResizeEmbeddedApp();
        return size;
    }
}
</code></pre>