示例#1
0
        private static IEnumerable <IMonitor> EnumerateMonitors(List <DeviceItemPlus> deviceItems)
        {
            if (!(deviceItems?.Any() == true))
            {
                yield break;
            }

            // Obtained by DDC/CI
            foreach (var handleItem in DeviceContext.GetMonitorHandles())
            {
                foreach (var physicalItem in MonitorConfiguration.EnumeratePhysicalMonitors(handleItem.MonitorHandle))
                {
                    int index = -1;
                    if (physicalItem.IsSupported)
                    {
                        index = deviceItems.FindIndex(x =>
                                                      (x.DisplayIndex == handleItem.DisplayIndex) &&
                                                      (x.MonitorIndex == physicalItem.MonitorIndex) &&
                                                      string.Equals(x.Description, physicalItem.Description, StringComparison.OrdinalIgnoreCase));
                    }
                    if (index < 0)
                    {
                        physicalItem.Handle.Dispose();
                        continue;
                    }

                    var deviceItem = deviceItems[index];
                    yield return(new DdcMonitorItem(
                                     deviceInstanceId: deviceItem.DeviceInstanceId,
                                     description: deviceItem.AlternateDescription,
                                     displayIndex: deviceItem.DisplayIndex,
                                     monitorIndex: deviceItem.MonitorIndex,
                                     handle: physicalItem.Handle,
                                     useLowLevel: physicalItem.IsLowLevelSupported));

                    deviceItems.RemoveAt(index);
                    if (deviceItems.Count == 0)
                    {
                        yield break;
                    }
                }
            }

            // Obtained by WMI
            var installedItems = DeviceInstallation.EnumerateInstalledMonitors().ToArray();

            foreach (var desktopItem in MSMonitor.EnumerateDesktopMonitors())
            {
                foreach (var installedItem in installedItems)
                {
                    int index = -1;
                    if (desktopItem.BrightnessLevels.Any())
                    {
                        index = deviceItems.FindIndex(x =>
                                                      string.Equals(x.DeviceInstanceId, desktopItem.DeviceInstanceId, StringComparison.OrdinalIgnoreCase) &&
                                                      string.Equals(x.DeviceInstanceId, installedItem.DeviceInstanceId, StringComparison.OrdinalIgnoreCase));
                    }
                    if (index < 0)
                    {
                        continue;
                    }

                    var deviceItem = deviceItems[index];
                    yield return(new WmiMonitorItem(
                                     deviceInstanceId: deviceItem.DeviceInstanceId,
                                     description: deviceItem.AlternateDescription,
                                     displayIndex: deviceItem.DisplayIndex,
                                     monitorIndex: deviceItem.MonitorIndex,
                                     brightnessLevels: desktopItem.BrightnessLevels,
                                     isRemovable: installedItem.IsRemovable));

                    deviceItems.RemoveAt(index);
                    if (deviceItems.Count == 0)
                    {
                        yield break;
                    }
                }
            }

            // Unreachable neither by DDC/CI nor by WMI
            foreach (var deviceItem in deviceItems)
            {
                yield return(new UnreachableMonitorItem(
                                 deviceInstanceId: deviceItem.DeviceInstanceId,
                                 description: deviceItem.AlternateDescription,
                                 displayIndex: deviceItem.DisplayIndex,
                                 monitorIndex: deviceItem.MonitorIndex,
                                 isInternal: deviceItem.IsInternal));
            }
        }