wpf 获取窗口所在屏幕 根据父窗口所在屏幕定位子窗口

 public enum HorizontalLocate
    {
        LEFT,

        CENTER,

        RIGHT
    }
    public enum VerticalLocate
    {
        TOP,

        CENTER,

        BOTTOM
    }
    public static class WindowLocateHelper
    {
        /// <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;
        }
    }

wpf 获取窗口所在屏幕 根据父窗口所在屏幕定位子窗口

上一篇:win10 环境中 RocketMq环境搭建


下一篇:关于Unknown system variable 'query_cache_size'的问题