• 周日. 9月 25th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

wpf 获取窗口所在屏幕 根据父窗口所在屏幕定位子窗口 将窗口显示在指定的屏幕上 获取第一块不包含指定窗口的屏幕

admin

11月 28, 2021
 public enum HorizontalLocate
    {
        LEFT,

        CENTER,

        RIGHT
    }
    public enum VerticalLocate
    {
        TOP,

        CENTER,

        BOTTOM
    }
    public static class WindowLocateHelper
    {

        /// <summary>
        /// 将窗口显示在指定的屏幕上
        /// </summary>
        /// <param name="window"></param>
        /// <param name="screen"></param>
        public static void ShowOnScreen(this Window window, System.Windows.Forms.Screen screen)
        {
            window.Left = screen.WorkingArea.Left;
            window.Top = screen.WorkingArea.Top;
        }

/// <summary>
        /// 获取第一块不包含指定窗口的屏幕
        /// </summary>
        /// <param name="window"></param>
        public static System.Windows.Forms.Screen OtherScreens(this Window window)
        {
            System.Windows.Forms.Screen displayScreen = null;
            var windowScreen = window.GetWindowScreen();

            foreach (var screen in System.Windows.Forms.Screen.AllScreens)
            {
                if (windowScreen.DeviceName != screen.DeviceName)
                {
                    displayScreen = screen;
                    break;
                }
            }

            if (displayScreen == null)
            {
                displayScreen = System.Windows.Forms.Screen.AllScreens.FirstOrDefault();
            }

            return displayScreen;
        }

        /// <summary>
        /// 获取窗口所在的屏幕
        /// </summary>
        /// <param name="reletiveWindow"></param>
        /// <returns></returns>
        private static System.Windows.Forms.Screen GetWindowScreen(this Window reletiveWindow)
        {
            IntPtr handle = new WindowInteropHelper(reletiveWindow).Handle;
            var screen = System.Windows.Forms.Screen.FromHandle(handle);
            return screen;
        }

        /// <summary>
        /// 将窗口定位到依赖窗口所属屏幕的指定位置
        /// </summary>
        /// <param name="subWindow">子窗口</param>
        /// <param name="reletiveWindow">定位所依赖的父窗口</param>
        /// <param name="horizontalLocate"></param>
        /// <param name="verticalLocate"></param>
        public static void Locate(this Window subWindow, Window reletiveWindow, HorizontalLocate horizontalLocate, VerticalLocate verticalLocate, double marginHorizontal = 0, double marginVertical = 0)
        {
            Screen screen = GetWindowScreen(reletiveWindow);

            //重新定位
            switch (horizontalLocate)
            {
                case HorizontalLocate.LEFT:
                    CalcLocation(subWindow, screen, false, LocateInnerType.BEGIN, marginHorizontal);
                    break;
                case HorizontalLocate.CENTER:
                    CalcLocation(subWindow, screen, false, LocateInnerType.MIDDLE, marginHorizontal);
                    break;
                case HorizontalLocate.RIGHT:
                    CalcLocation(subWindow, screen, false, LocateInnerType.END, marginHorizontal);
                    break;
                default:
                    break;
            }

            switch (verticalLocate)
            {
                case VerticalLocate.TOP:
                    CalcLocation(subWindow, screen, true, LocateInnerType.BEGIN, marginVertical);
                    break;
                case VerticalLocate.CENTER:
                    CalcLocation(subWindow, screen, true, LocateInnerType.MIDDLE, marginVertical);
                    break;
                case VerticalLocate.BOTTOM:
                    CalcLocation(subWindow, screen, true, LocateInnerType.END, marginVertical);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 将水平、垂直定位翻译为统一的开始、居中、结束位置
        /// </summary>
        private enum LocateInnerType
        {
            BEGIN,

            MIDDLE,

            END
        }
        /// <summary>
        /// 定位窗口
        /// </summary>
        /// <param name="isVertical"></param>
        /// <param name="targetScreen"></param>
        /// <param name="margin">边距</param>
        private static void CalcLocation(Window window, Screen targetScreen, bool isVertical, LocateInnerType locateType, double margin = 0)
        {
            var windowLength = isVertical ? window.Height : window.Width;//窗口宽高
            double startLocate = 0;//定位目标位置
            double screenLocate = isVertical ? targetScreen.Bounds.Top : targetScreen.Bounds.Left;//屏幕坐标
            double screenLength = isVertical ? targetScreen.Bounds.Height : targetScreen.Bounds.Width;//屏幕宽高

            switch (locateType)
            {
                case LocateInnerType.BEGIN:
                    startLocate = screenLocate + margin;
                    break;
                case LocateInnerType.MIDDLE:
                    startLocate = screenLocate + screenLength / 2 - windowLength / 2;
                    break;
                case LocateInnerType.END:
                    startLocate = screenLocate + screenLength - windowLength - margin;
                    break;
                default:
                    break;
            }

            if (isVertical)
            {
                window.Top = startLocate;
            }
            else
            {
                window.Left = startLocate;
            }
        }

        /// <summary>
        /// 获取窗口所在的屏幕
        /// </summary>
        /// <param name="reletiveWindow"></param>
        /// <returns></returns>
        private static Screen GetWindowScreen(Window reletiveWindow)
        {
            IntPtr handle = new WindowInteropHelper(reletiveWindow).Handle;
            var screen = Screen.FromHandle(handle);
            return screen;
        }
    }
留待后查,同时方便他人
联系我:[email protected]

发表回复

您的电子邮箱地址不会被公开。