Esempio n. 1
0
        private static bool TryGetDisplay(string deviceInstanceId, out string displayName, out DEVMODE dm)
        {
            foreach (var(display, _, monitor, _) in EnumerateDevices())
            {
                if (!string.Equals(DeviceConversion.ConvertToDeviceInstanceId(monitor.DeviceID), deviceInstanceId, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                displayName = display.DeviceName;
                dm          = new DEVMODE {
                    dmSize = (ushort)Marshal.SizeOf <DEVMODE>()
                };

                // EnumDisplaySettings function only works with display device.
                return(EnumDisplaySettings(
                           displayName,
                           ENUM_CURRENT_SETTINGS,
                           ref dm));
            }

            displayName = null;
            dm          = default;
            return(false);
        }
Esempio n. 2
0
        public static IEnumerable <DisplayItem> EnumerateDisplayConfigs()
        {
            if (GetDisplayConfigBufferSizes(
                    QDC_ONLY_ACTIVE_PATHS,
                    out uint pathCount,
                    out uint modeCount) != ERROR_SUCCESS)
            {
                yield break;
            }

            var displayPaths = new DISPLAYCONFIG_PATH_INFO[pathCount];
            var displayModes = new DISPLAYCONFIG_MODE_INFO[modeCount];

            if (QueryDisplayConfig(
                    QDC_ONLY_ACTIVE_PATHS,
                    ref pathCount,
                    displayPaths,
                    ref modeCount,
                    displayModes,
                    IntPtr.Zero) != ERROR_SUCCESS)
            {
                yield break;
            }

            foreach (var displayPath in displayPaths)
            {
                var displayMode = displayModes
                                  .Where(x => x.infoType == DISPLAYCONFIG_MODE_INFO_TYPE.DISPLAYCONFIG_MODE_INFO_TYPE_TARGET)
                                  .FirstOrDefault(x => x.id == displayPath.targetInfo.id);
                if (displayMode.Equals(default(DISPLAYCONFIG_MODE_INFO)))
                {
                    continue;
                }

                var deviceName = new DISPLAYCONFIG_TARGET_DEVICE_NAME();
                deviceName.header.size      = (uint)Marshal.SizeOf <DISPLAYCONFIG_TARGET_DEVICE_NAME>();
                deviceName.header.adapterId = displayMode.adapterId;
                deviceName.header.id        = displayMode.id;
                deviceName.header.type      = DISPLAYCONFIG_DEVICE_INFO_TYPE.DISPLAYCONFIG_DEVICE_INFO_GET_TARGET_NAME;

                if (DisplayConfigGetDeviceInfo(ref deviceName) != ERROR_SUCCESS)
                {
                    continue;
                }

                var deviceInstanceId = DeviceConversion.ConvertToDeviceInstanceId(deviceName.monitorDevicePath);

                yield return(new DisplayItem(
                                 deviceInstanceId: deviceInstanceId,
                                 displayName: deviceName.monitorFriendlyDeviceName,
                                 isInternal: (deviceName.outputTechnology == DISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY.DISPLAYCONFIG_OUTPUT_TECHNOLOGY_INTERNAL),
                                 connectionDescription: GetConnectionDescription(deviceName.outputTechnology),
                                 isAvailable: displayPath.targetInfo.targetAvailable));
            }
        }
Esempio n. 3
0
        public static IEnumerable <DeviceItem> EnumerateMonitorDevices()
        {
            foreach (var(_, displayIndex, monitor, monitorIndex) in EnumerateDevices())
            {
                var deviceInstanceId = DeviceConversion.ConvertToDeviceInstanceId(monitor.DeviceID);

                //Debug.WriteLine($"DeviceId: {monitor.DeviceID}");
                //Debug.WriteLine($"DeviceInstanceId: {deviceInstanceId}");
                //Debug.WriteLine($"DeviceString: {monitor.DeviceString}");

                yield return(new DeviceItem(
                                 deviceInstanceId: deviceInstanceId,
                                 description: monitor.DeviceString,
                                 displayIndex: displayIndex,
                                 monitorIndex: monitorIndex));
            }
        }