Example #1
0
        public static List <MonitorInfo> GetMonitors()
        {
            List <MonitorInfo> result = new List <MonitorInfo>();

            MonitorEnumDelegate med = (IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData) =>
            {
                var minfo = GetMonitorInfo(hMonitor);
                result.Add(minfo);
                return(true);
            };

            Monitors.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, med, IntPtr.Zero);

            return(result);
        }
Example #2
0
        static void Window_LocationChanged(object sender, EventArgs e)
        {
            Window win = (Window)sender;

            var info = Monitors.GetMonitorFromWindow(win, NotFoundOptions.DefaultToNearest);

            if (win.MaxWidth != info.WorkingArea.Width || win.MaxHeight != info.WorkingArea.Height)
            {
                if (win.MaxWidth != double.PositiveInfinity && win.MaxHeight != double.PositiveInfinity)
                {
                    SetJustChangedMonitor(win, DateTime.Now);
                }

                win.MaxWidth  = info.WorkingArea.Width;
                win.MaxHeight = info.WorkingArea.Height;
            }
        }
Example #3
0
        static MonitorInfo GetMonitorInfo(IntPtr hMonitor)
        {
            MONITORINFO mi = new MONITORINFO();

            mi.size = (uint)Marshal.SizeOf(mi);
            if (!Monitors.GetMonitorInfo(hMonitor, ref mi))
            {
                return(null);
            }

            return(new MonitorInfo
            {
                Handler = hMonitor,
                IsPrimary = mi.flags != 0,
                DisplayArea = mi.monitor.ToRect(),
                WorkingArea = mi.work.ToRect(),
            });
        }
Example #4
0
        static void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Window win = (Window)sender;

            if (win.SizeToContent != SizeToContent.WidthAndHeight)
            {
                return;
            }

            if (!GetCenterOnSizedToContent(win))
            {
                return;
            }

            var dt = GetJustChangedMonitor(win);

            if (dt.HasValue)
            {
                if (dt.Value.Add(TimeSpan.FromSeconds(1)) < DateTime.Now)
                {
                    SetJustChangedMonitor(win, null);
                }
                else
                {
                    return;
                }
            }

            var info = Monitors.GetMonitorFromWindow(win, NotFoundOptions.DefaultToNearest);

            if (info.WorkingArea.Contains(new Rect(win.Left, win.Top, e.NewSize.Width, e.NewSize.Height)))
            {
                return;
            }

            win.Left = info.WorkingArea.Left + (info.WorkingArea.Width / 2) - (e.NewSize.Width / 2);
            win.Top  = info.WorkingArea.Top + (info.WorkingArea.Height / 2) - (e.NewSize.Height / 2);
        }