1、引用Win32API
/// <summary>
/// 禁用窗口
/// </summary>
/// <param name="hWnd"></param>
/// <param name="bEnable"></param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern bool EnableWindow(IntPtr hWnd, bool bEnable);//设置Enable属性
/// <summary>
/// 禁用窗口
/// </summary>
/// <param name="hWnd"></param>
/// <param name="bEnable"></param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern bool EnableWindow(IntPtr hWnd, bool bEnable);//设置Enable属性
2、在show之前
Window _Window = new Window();
DispatcherFrame frame = new DispatcherFrame(true);
_Window.ShowActivated = true;
IntPtr parentIntPtr = IntPtr.Zero;
if (_Window.Owner != null)
{
parentIntPtr = new WindowInteropHelper(_Window.Owner).Handle;
}
EnableWindow(parentIntPtr, false);
_Window.Closed += (s1, e1) =>
{
ComponentDispatcher.PopModal();
frame.Continue = false;
EnableWindow(parentIntPtr, true);
if (_Window.Owner != null)
_Window.Owner.Activate();
};
_Window.Show();
_Window.Activate();
Window _Window = new Window();
DispatcherFrame frame = new DispatcherFrame(true);
_Window.ShowActivated = true;
IntPtr parentIntPtr = IntPtr.Zero;
if (_Window.Owner != null)
{
parentIntPtr = new WindowInteropHelper(_Window.Owner).Handle;
}
EnableWindow(parentIntPtr, false);
_Window.Closed += (s1, e1) =>
{
ComponentDispatcher.PopModal();
frame.Continue = false;
EnableWindow(parentIntPtr, true);
if (_Window.Owner != null)
_Window.Owner.Activate();
};
_Window.Show();
_Window.Activate();
try
{
ComponentDispatcher.PushModal();
Dispatcher.PushFrame(frame);//show之后阻塞,以实现showdialog的效果
}
finally
{
ComponentDispatcher.PopModal();
}
{
ComponentDispatcher.PushModal();
Dispatcher.PushFrame(frame);//show之后阻塞,以实现showdialog的效果
}
finally
{
ComponentDispatcher.PopModal();
}
使用这种方式,再打开window之后,只禁用弹出窗口的父窗口;而使用ShowDialog()则会禁用弹窗之外的所有窗口