Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在模式模式下显示来自其他进程的WPF窗口_Wpf - Fatal编程技术网

在模式模式下显示来自其他进程的WPF窗口

在模式模式下显示来自其他进程的WPF窗口,wpf,Wpf,我有两个WPF应用程序和一个流程管理器,它们将数据从第一个WPF应用程序传递到第二个WPF应用程序,反之亦然。在一个用例中,我必须以模态模式在第二个应用程序的窗口(主窗口)上显示第一个应用程序的窗口(主窗口)。因此,第二个WPF应用程序的窗口将被禁用,并且第一个WPF应用程序的窗口顶部将显示。所需的行为与在单个WPF应用程序中以模态模式显示窗口相同。知道如何从另一个WPF应用程序访问一个WPF应用程序的窗口吗 对于Winform应用程序,我们通过将窗口句柄(intPtr)传递给另一个应用程序来完

我有两个WPF应用程序和一个流程管理器,它们将数据从第一个WPF应用程序传递到第二个WPF应用程序,反之亦然。在一个用例中,我必须以模态模式在第二个应用程序的窗口(主窗口)上显示第一个应用程序的窗口(主窗口)。因此,第二个WPF应用程序的窗口将被禁用,并且第一个WPF应用程序的窗口顶部将显示。所需的行为与在单个WPF应用程序中以模态模式显示窗口相同。知道如何从另一个WPF应用程序访问一个WPF应用程序的窗口吗

对于Winform应用程序,我们通过将窗口句柄(intPtr)传递给另一个应用程序来完成此操作,并在模式模式下显示窗口时使用如下句柄:

System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window)

在WPF应用程序中如何实现类似的事情?提前感谢。

=====================================开始更新=====================================

using System.Windows; // Window, WindowStartupLocation
using System.Windows.Interop; // WindowInteropHelper
...
// Instantiate the owned WPF window
CenteredWindow cw = new CenteredWindow();

// Get the handle to the non-WPF owner window
IntPtr ownerWindowHandle = ...; // Get hWnd for non-WPF window

// Set the owned WPF window’s owner with the non-WPF owner window
WindowInteropHelper helper = new WindowInteropHelper(cw);
helper.Owner = ownerWindowHandle;
cw.ShowDialog();
代码: 在@kamal-nayan的评论的帮助下,我如上所述修改了我的代码,它对我来说运行良好

关键是禁用父窗口,当模式对话框关闭时,启用父窗口


===========================================结束更新=====================================

using System.Windows; // Window, WindowStartupLocation
using System.Windows.Interop; // WindowInteropHelper
...
// Instantiate the owned WPF window
CenteredWindow cw = new CenteredWindow();

// Get the handle to the non-WPF owner window
IntPtr ownerWindowHandle = ...; // Get hWnd for non-WPF window

// Set the owned WPF window’s owner with the non-WPF owner window
WindowInteropHelper helper = new WindowInteropHelper(cw);
helper.Owner = ownerWindowHandle;
cw.ShowDialog();
这是我找到的唯一解决办法。 它不是真正的模式,也就是说,您仍然可以激活父窗口,但好的是子窗口仍然位于父窗口的顶部


这就是我如何将一个进程中的窗口显示为模态窗口,并将另一个进程中的窗口显示为模态窗口。

是您想要的吗?不。我的要求不同。这不是我想要的。我试过这个,但它不像真正的模态。我将添加我目前用于实现此目的的代码。我将在这方面花费更多的时间,看看它是否有效。我会再试一次。这个链接有更多的功能。它在打开模式模式后禁用父窗口,然后再次启用它。这可能会有帮助。谢谢,我稍微修改了你的代码,因为HwndSourceParameters的构造函数需要一个字符串参数。它对我不起作用,可能我遗漏了什么。我会再调查的。是的,可能不起作用。它不是笔直的,特别是指定根视觉。它需要内容。它不接受窗口。这需要很多变通方法。我还尝试删除这段代码。更新,在我的测试虚拟机中,使用HwndSourceParameters的代码与使用WindowInteropHelper的代码的工作原理相同。我想我可能需要设置一些窗口样式或前样式,不确定。因为winform one可以工作,所以在技术上是可能的。另一种可能的解决方案是在WinForm中使用ElementHost托管WPF用户控件。您的ms connect链接非常有用,我已经更新了代码,现在它对我有效,请参阅我的更新。
_parameters = new HwndSourceParameters("myWindow");

_parameters.WindowStyle = WindowStyles.WS_SYSMENU | WindowStyles.WS_VISIBLE | WindowStyles.WS_CAPTION | WindowStyles.WS_CHILD | WindowStyles.WS_POPUP;
_parameters.SetPosition(50, 50);

_parameters.ParentWindow = ParentWindowHandle;           
_hwndSource = new HwndSource(_parameters);

_hwndSource.SizeToContent = SizeToContent.WidthAndHeight;
_hwndSource.RootVisual = modalWindowContent;