![](/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.