1

我需要在我的 wpf 应用程序上封装 exe。我的 wpf 应用程序非常大并且有许多 UserControls。为此,我从我的代码中启动 exe,然后获取句柄并使用“setParent”将 exe“绑定”到我的应用程序,但唯一的效果是显示 exe 的下拉菜单,但是不是主页。例如:我尝试嵌入记事本,但是当我单击该区域时只出现下拉菜单(请注意,不会出现主菜单栏)。

  var procInfo = new System.Diagnostics.ProcessStartInfo(this.exeName);
  procInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(this.exeName);

  // Start the process
  _childp = System.Diagnostics.Process.Start(procInfo);

  // Wait for process to be created and enter idle condition
  _childp.WaitForInputIdle();

  // Get the main handle
  _appWin = _childp.MainWindowHandle;

  // Get main window handle
  var helper = new WindowInteropHelper(Window.GetWindow(this.AppContainer));

  // Incapsulate
  SetWindowLongA(_appWin, -20, 0x00000040 | 0x00000008);
  SetParent(_appWin, helper.Handle);

请注意,我已经在其他 c# 应用程序中尝试过这段代码并且工作正常!我认为重绘/更新视口存在问题。我可以通过哪种方式强制重绘我的应用程序中的外部 exe?你能帮助我,甚至找到嵌入 exe 的替代解决方案吗?谢谢

在此处输入图像描述

我已经尝试过在单独的选项卡(此处)中运行 exe 的解决方案,但即使这个解决方案也不起作用。

我可以用“SendMessage”解决这个问题吗???你能建议我做一个测试吗?

我问你一件事:救救我!!!

4

2 回答 2

0

在窗口的 WPF 中使用AllowsTransparency="False"而不是 AllowsTransparency="True" 我已经能够部分解决问题

现在我使用这种方法(WindowsFormHost方法)嵌入了外部 exe(例如:“notepad.exe”):

System.Windows.Forms.Panel _pnlSched = new System.Windows.Forms.Panel();

System.Windows.Forms.Integration.WindowsFormsHost windowsFormsHost1 = 
              new System.Windows.Forms.Integration.WindowsFormsHost();
windowsFormsHost1.Child = _pnlSched;
_grid.Children.Add(windowsFormsHost1);
ProcessStartInfo psi = new ProcessStartInfo(@"notepad.exe");
psi.WindowStyle = ProcessWindowStyle.Normal;
Process PR = Process.Start(psi);
PR.WaitForInputIdle(); 
SetParent(PR.MainWindowHandle, _pnlSched.Handle);  

现在的问题可能是用户控件的 Z 顺序。事实上,当另一个用户控制移动到“记事本”上方时,它在下方而不是上方......

在此处输入图像描述

请注意,WindowsFormHost 的背景也不尊重“z 顺序”:

在此处输入图像描述

欢迎任何建议

谢谢

于 2016-06-23T10:40:07.200 回答
0

以下对我有用,如有必要,可以为您提供示例项目。缺少的部分似乎是您有 az index 问题,或者您在桌面坐标中的初始窗口放置是这样的,它位于您的“外部窗口”之外。

这将把它带到 from 并让它填满你的窗口:

SetWindowPos(_appWin, default(IntPtr), 0, 0, (int)Application.Current.MainWindow.Width, (int)Application.Current.MainWindow.Height, SetWindowPosFlags.FrameChanged);

默认值(IntPtr)用于 ZIndex,并表示“放在前面”

然后,您可以通过传入包含控件的偏移量来使其更小,即如果this.grid您希望记事本出现在上面:

    var desiredPos = this.grid.TranslatePoint(new Point(0, 0), Window.GetWindow(this.grid));
    SetWindowPos(_appWin, default(IntPtr), 
        (int)desiredPos.X, (int)desiredPos.Y, 
        (int)this.grid.ActualWidth, (int)this.grid.ActualHeight, SetWindowPosFlags.FrameChanged);
于 2016-06-10T14:40:59.023 回答