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