예제 #1
0
        public void Initialize()
        {
            monitors = new List <Monitor>();

            physicalMonitors = new List <PHYSICAL_MONITOR>();


            List <(IntPtr monitor, Rect rect)> hMonitors = new List <(IntPtr, Rect)>();

            MonitorEnumProc callback = (IntPtr hMonitor, IntPtr hdc, ref Rect prect, int d) =>
            {
                monitors.Add(new Monitor
                {
                    hMonitor = hMonitor,
                    rect     = prect,
                });
                return(true);
            };


            if (EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, 0))
            {
                foreach (var m in monitors)
                {
                    uint mcount = 0;
                    if (!GetNumberOfPhysicalMonitorsFromHMONITOR(m.hMonitor, ref mcount))
                    {
                        throw new Exception("Cannot get monitor count!");
                    }
                    PHYSICAL_MONITOR[] physicalMonitors = new PHYSICAL_MONITOR[mcount];

                    if (!GetPhysicalMonitorsFromHMONITOR(m.hMonitor, mcount, physicalMonitors))
                    {
                        throw new Exception("Cannot get phisical monitor handle!");
                    }

                    Debug.WriteLine($"PM:{physicalMonitors.Length}) RECT: T:{m.rect.Top}/L:{m.rect.Left}/R:{m.rect.Right}/B:{m.rect.Bottom}");

                    this.physicalMonitors.AddRange(physicalMonitors);

                    m.physicalMonitors = physicalMonitors.Select(a => new PhysicalMonitor
                    {
                        DeviceName       = a.szPhysicalMonitorDescription,
                        hPhysicalMonitor = a.hPhysicalMonitor
                    }).ToList();
                }

                foreach (var p in monitors.SelectMany(a => a.physicalMonitors))
                {
                    uint cv = 0;

                    // power
                    if (GetFeatureValue(p.hPhysicalMonitor, SVC_FEATURE__POWER_MODE, ref cv))
                    {
                        p.IsPoweredOn = (cv == (uint)PowerModeEnum.PowerOn);
                        Debug.WriteLine($"{p.hPhysicalMonitor} + {p.DeviceName} + POWER={cv}");
                        p.IsEnabled = true;
                    }
                    else
                    {
                        string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
                        Debug.WriteLine($"ERROR for {p.DeviceName}: `{errorMessage}`");
                    }

                    // BRIG
                    if (GetFeatureValue(p.hPhysicalMonitor, SVC_FEATURE__BRIGHTNESS, ref cv))
                    {
                        p.BrightnessLevel = cv;
                        Debug.WriteLine($"{p.hPhysicalMonitor} + {p.DeviceName} + BRIGHTNESS={cv}");
                    }
                    else
                    {
                        string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
                        Debug.WriteLine($"ERROR for {p.DeviceName}: `{errorMessage}`");
                    }
                }
            }
        }