2016-06-10 35 views
1

我需要将exe封装在我的wpf应用程序中。 我的wpf应用程序非常大,并有许多UserControls。 要做到这一点,我已经从我的代码启动exe,然后获取句柄,并使用“setParent”将exe绑定到我的应用程序,但唯一的影响是显示exe的下拉菜单,但不是主页面。例如:我试图嵌入记事本,但当我点击该区域时只出现下拉菜单(请注意,不出现在主菜单栏中)。在wpf中的c#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文件吗?由于

enter image description here

我试图运行在一个单独的标签(here)exe文件的解决方案,但即使这个解决方案行不通。

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

我问你一件事:帮帮我!

+0

HTTP:/ /www.pinvoke.net/default.aspx/user32.RedrawWindow? – tolanj

+0

或者这是Z指数的问题?我不是100%确定你正在努力实现什么...... – tolanj

+0

http://www.pinvoke.net/default.aspx/user32/BringWindowToTop.html ?? – tolanj

回答

0

以下适用于我,如有必要可以为您提供示例项目。缺失的部分似乎是你有z索引问题,或者你的桌面co-oridinates中的初始窗口位置是这样的,它在你的“外部窗口”之外。

这将在从把它和让它FILL你的窗口:

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

默认(IntPtr的)是Z-索引并说“带来正面”

然后,您可以作出这样的较小通过从包含控制,即在偏移如果通过了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); 
+0

此解决方案不起作用.... – ste

+0

它适用于我的一个简单的项目示例包装记事本,没有您的代码的完整版本我不能真的走得更远,你有例如有代码在哪里你正在尝试上述解决方案? – tolanj

0

使用AllowsTransparency =“假”代替AllowsTransparency =“真”的WPF窗口的我已经能够部分地解决问题

里面现在,我已经嵌入外部exe文件(例如:“记事本。使用这种方法(WindowsFormHost的方法)的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次序。事实上,当另一个用户控制移动到“记事本”上方时,它低于并且不高于...

enter image description here

注意,也WindowsFormHost的背景不尊重 'Z顺序':

enter image description here

任何建议,欢迎

感谢

+0

这是原因:http://stackoverflow.com/questions/9920480/windowsformshost-is-always-the-most-top-from-wpf-element – ste