我的问题是如何在WPF应用程序内运行一个应用程序(.exe)。我的意思是在应用程序的窗口内运行,而不是外部运行应用程序。
提前感谢:D
提前感谢:D
HwndHost
并且重写 BuildWindowCore()
方法的类。请看 Microsoft 的示例,链接为 http://msdn.microsoft.com/en-us/library/ms752055.aspx。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;
}
[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()即可。但是,如果您尝试将应用程序嵌入选项卡页中,请小心。您会在那里遇到一些有趣的问题。