using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows;

namespace AIRS
{
    class DialogHelper
    {
        //从Handle中获取Window对象
        static Window GetWindowFromHwnd(IntPtr hwnd)
        {
            try
            {
                return (Window)HwndSource.FromHwnd(hwnd).RootVisual;
            }
            catch
            {
                return null;
            }
        }

        //GetForegroundWindow API
        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();

        //调用GetForegroundWindow然后调用GetWindowFromHwnd
        static Window GetTopWindow()
        {
            var hwnd = GetForegroundWindow();
            if (hwnd == null)
                return null;

            return GetWindowFromHwnd(hwnd);
        }

        //显示对话框并自动设置Owner，可以理解为子窗体添加父窗体
        public static void ShowDialog(Window win)
        {
            win.Owner = GetTopWindow();
            win.ShowInTaskbar = false;       //false表示不显示新的窗口，默认当前打开窗口为一个子窗口（不会显示两个窗口）
            win.ShowDialog();
        }
    }
}
