Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you are looking to do is entirely possible, but it does come with a few bugs, so don't expect an easy ride. What you need to do is create a class that inherits from <code>HwndHost</code> and overrides the <code>BuildWindowCore()</code> method. See the example from Microsoft at <a href="http://msdn.microsoft.com/en-us/library/ms752055.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms752055.aspx</a></p> <p>In the above example, they give you the notion of being able to put a Win32 control within your WPF form, but you can use the same architecture to load the <code>MainWindowhandle</code> of your created Notepad process into the child of the border. Like this:</p> <pre><code>protected override HandleRef BuildWindowCore(HandleRef hwndParent) { ProcessStartInfo psi = new ProcessStartInfo("notepad.exe"); psi.WindowStyle = ProcessWindowStyle.Minimized; _process = Process.Start(psi); _process.WaitForInputIdle(); // The main window handle may be unavailable for a while, just wait for it while (_process.MainWindowHandle == IntPtr.Zero) { Thread.Yield(); } IntPtr notepadHandle = _process.MainWindowHandle; int style = GetWindowLong(notepadHandle, GWL_STYLE); style = style &amp; ~((int)WS_CAPTION) &amp; ~((int)WS_THICKFRAME); // Removes Caption bar and the sizing border style |= ((int)WS_CHILD); // Must be a child window to be hosted SetWindowLong(notepadHandle, GWL_STYLE, style); SetParent(notepadHandle, hwndParent.Handle); this.InvalidateVisual(); HandleRef hwnd = new HandleRef(this, notepadHandle); return hwnd; } </code></pre> <p>Please keep in mind that you will need to import a few functions from the User32.dll to be able to set the style of the window and set the parent window handle:</p> <pre><code>[DllImport("user32.dll")] private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); [DllImport("user32.dll", SetLastError = true)] private static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32")] private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent); </code></pre> <p>Also make sure that you are including:</p> <pre><code>using System.Windows.Interop; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; </code></pre> <p>This is my first answer on a forum so please let me know if it needs some work. Also reference <a href="https://stackoverflow.com/questions/5028598/hosting-external-app-in-wpf-window">Hosting external app in WPF window</a> but don't worry about DwayneNeed stuff. Just use SetParent() as you see in my code above. Just be careful if you try embedding the app inside a tab page. You will encounter some fun there.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload