• 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()则会禁用弹窗之外的所有窗口
  • 相关阅读:
    ArrayList 和 Vector 的区别
    Redis在springboot中使用,读取字符串
    初始化Mysql
    Redis 安装
    React-脚手架
    React virtual DOM explained in simple English/简单语言解释React的虚拟DOM
    数据结构
    书单(18-19)
    算法复杂度
    otrs离线部署
  • 原文地址:https://www.cnblogs.com/JqkAman/p/12626953.html
Copyright © 2020-2023  润新知