在我的WPF / C#应用程序中,我正在使用如下代码创建一个对话框窗口:
Window dialog = new MyDialog() as Window;
dialog.Owner = Window.GetWindow(this);
dialog.ShowDialog();
如何将对话框所有者设置为另一个应用程序窗口的hWnd?
我需要的功能就是在对话框可见时阻止“所有者窗口”。
答案 0 :(得分:15)
Window dialog = new MyDialog();
WindowInteropHelper wih = new WindowInteropHelper(dialog);
wih.Owner = ownerHwnd;
dialog.ShowDialog();
答案 1 :(得分:9)
我找到了阻止“所有者窗口”的解决方案。代码的第一部分来自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);