wpf 使用 hWnd 设置 Window.Owner
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13842869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Window.Owner using hWnd
提问by Drahcir
In my WPF/C# app I'm creating a dialog window using code like the below:
在我的 WPF/C# 应用程序中,我使用如下代码创建一个对话框窗口:
Window dialog = new MyDialog() as Window;
dialog.Owner = Window.GetWindow(this);
dialog.ShowDialog();
How can I set the dialog owner to the hWnd of another applications window?
如何将对话框所有者设置为另一个应用程序窗口的 hWnd?
The functionality that I need is just to have the "Owner Window" to be blocked while the dialog is visible.
我需要的功能只是在对话框可见时阻止“所有者窗口”。
采纳答案by Drahcir
I have found a solution to block the "Owner Window". The first part of the code is from Douglas answer, the rest is using a call to the WinAPI EnableWindowmethod:
我找到了阻止“所有者窗口”的解决方案。代码的第一部分来自 Douglas 的回答,其余部分是使用对 WinAPI EnableWindow方法的调用:
Window dialog = new MyDialog();
WindowInteropHelper wih = new WindowInteropHelper(dialog);
wih.Owner = ownerHwnd;
//Block input to the owner
Windows.EnableWindow(ownerHwnd, false);
EventHandler onClosed = null;
onClosed = (object sender, EventArgs e) =>
{
//Re-Enable the owner window once the dialog is closed
Windows.EnableWindow(ownerHwnd, true);
(sender as Window).closed -= onClosed;
};
dialog.Closed += onClosed;
dialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
dialog.ShowActivated = true;
dialog.Show();
//Import the EnableWindow method
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnableWindow(IntPtr hWnd, bool bEnable);
回答by Douglas
Use WindowInteropHelper:
Window dialog = new MyDialog();
WindowInteropHelper wih = new WindowInteropHelper(dialog);
wih.Owner = ownerHwnd;
dialog.ShowDialog();

