我的問題是如何在WPF應用程序中運行應用程序(.exe)。 我的意思是在應用程序的窗口內運行,而不是在外部運行應用程序。
先謝謝了
我的問題是如何在WPF應用程序中運行應用程序(.exe)。 我的意思是在應用程序的窗口內運行,而不是在外部運行應用程序。
先謝謝了
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 HwndHost
and overrides the BuildWindowCore()
method. See the
example from Microsoft at http://msdn.microsoft.com/en-us/library/ms752055.aspx
在上面的示例中,它們為您提供了能夠將Win32控件放在WPF窗體中的概念,但是您可以使用相同的體系結構將創建的記事本進程的
MainWindowhandle
加載到子窗口中。邊界。喜歡這個:
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;
}
請記住,您需要從User32.dll導入一些函數,以便能夠設置窗口的樣式並設置父窗口句柄:
[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);
還要確保包括:
using System.Windows.Interop;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
這是我在論壇上的第一個答案,所以如果需要一些工作,請告訴我。另請參考在WPF窗口中托管外部應用程序,但不要擔心DwayneNeed的東西。只需使用上面代碼中的SetParent()。如果您嘗試在應用頁面中嵌入應用,請務必小心。你會在那裏遇到一些樂趣。