![](/img/trans.png)
[英]Run exe application inside WPF application, Different between Command and Click?
[英]How to run exe application inside wpf application?
我的问题是如何在 WPF 应用程序中运行应用程序 (.exe)。 我的意思是在应用程序的 window 中运行,而不是在外部运行应用程序,在此代码记事本中工作但如果 change.exe 文件(例如 calc.exe)应用程序在 exte 中打开
private Process _process;
[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);
[DllImport("user32")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
private const int SWP_NOZORDER = 0x0004;
private const int SWP_NOACTIVATE = 0x0010;
private const int GWL_STYLE = -16;
private const int WS_CAPTION = 0x00C00000;
private const int WS_THICKFRAME = 0x00040000;
private void LaunchChildProcess()
{
_process = Process.Start("notepad.exe");
_process.WaitForInputIdle();
var helper = new WindowInteropHelper(this);
while(_process.MainWindowHandle == IntPtr.Zero)
{
System.Threading.Thread.Sleep(10);
_process.Refresh();
}
SetParent(_process.MainWindowHandle, helper.Handle);
// remove control box
int style = GetWindowLong(_process.MainWindowHandle, GWL_STYLE);
style = style & ~WS_CAPTION & ~WS_THICKFRAME;
SetWindowLong(_process.MainWindowHandle, GWL_STYLE, style);
// resize embedded application & refresh
ResizeEmbeddedApp();
}
private void ResizeEmbeddedApp()
{
if (_process == null)
return;
SetWindowPos(_process.MainWindowHandle, IntPtr.Zero, 0, 0, (int)ActualWidth, (int)ActualHeight, SWP_NOZORDER | SWP_NOACTIVATE);
}
protected override Size MeasureOverride(Size availableSize)
{
Size size = base.MeasureOverride(availableSize);
ResizeEmbeddedApp();
return size;
}
一般来说,你不能。 正如您所发现的,只有一小部分 Win32 应用程序支持 window 重新设置父级。
您确实有一些选择:
DwmRegisterThumbnail
将其显示为应用程序顶部的叠加层。
DwmSetIconicThumbnail
- 除了它允许您在内存中的HBITMAP
中获取目标 window 。
HBITMAP
依赖于 GDI,并且 GDI 不支持 30 位颜色(也称为 10 位颜色)之类的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.