将窗体设置为窗口的所有者

Dra*_*nel 4 .net c# windows wpf winforms

我有一个围绕System.Windows.Forms.Form构建的主应用程序有一个用户可以调用的遗留System.Windows.Window,它被设置为Topmost.我希望Window由主应用程序拥有,因此它将最小化应用程序的最小化.所以我的预期代码是

TopmostDisplayWindow.Owner = MainAppForm;
Run Code Online (Sandbox Code Playgroud)

但是,没有Window.Owner方法采用Form和Window没有覆盖所有者的Show().

是否有一种简单的方法可以获得MainAppForm的窗口,还是需要更复杂的东西?

Rez*_*aei 5

您可以使用WindowInteropHelper和设置Owner它的属性:

var window = new YourWPFWindow();
WindowInteropHelper helper = new WindowInteropHelper(window);
helper.Owner = this.Handle;
window.Show();
Run Code Online (Sandbox Code Playgroud)

在Winform的主窗体中使用上面的代码Form.记住要添加所需的引用,当你想展示你的WPF窗口:PresentationCode,PresentationFramework,WindowsBase.