WPF Show實現ShowDialog的效果


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屬性
 
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();
try
{
    ComponentDispatcher.PushModal();
    Dispatcher.PushFrame(frame);//show之后阻塞,以實現showdialog的效果
}
finally
{
    ComponentDispatcher.PopModal();
}
使用這種方式,再打開window之后,只禁用彈出窗口的父窗口;而使用ShowDialog()則會禁用彈窗之外的所有窗口


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM