[ACCEPTED]-How to run an application inside wpf application?-wpf
What you are looking to do is entirely possible, but 21 it does come with a few bugs, so don't expect 20 an easy ride. What you need to do is create 19 a class that inherits from HwndHost
and overrides 18 the BuildWindowCore()
method. See the example from Microsoft 17 at http://msdn.microsoft.com/en-us/library/ms752055.aspx
In the above example, they give you the 16 notion of being able to put a Win32 control 15 within your WPF form, but you can use the 14 same architecture to load the MainWindowhandle
of your created 13 Notepad process into the child of the border. Like 12 this:
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 & ~((int)WS_CAPTION) & ~((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;
}
Please keep in mind that you will need 11 to import a few functions from the User32.dll 10 to be able to set the style of the window 9 and set the parent window handle:
[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);
Also make 8 sure that you are including:
using System.Windows.Interop;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
This is my first 7 answer on a forum so please let me know 6 if it needs some work. Also reference Hosting external app in WPF window but 5 don't worry about DwayneNeed stuff. Just 4 use SetParent() as you see in my code above. Just 3 be careful if you try embedding the app 2 inside a tab page. You will encounter some 1 fun there.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.