Exemplo n.º 1
0
        internal IEnumerable <DisplayDevice> GetDisplayDevices(bool?filterByValidity)
        {
            var device   = Native.DeviceContext.Structures.DisplayDevice.Initialize();
            var returned = new Dictionary <string, string>();

            for (uint i = 0; DeviceContextApi.EnumDisplayDevices(null, i, ref device, 0); i++)
            {
                if (DevicePath.Equals(device.DeviceId))
                {
                    DisplayDevice displayDevice = null;
                    var           display       = Native.DeviceContext.Structures.DisplayDevice.Initialize();
                    for (uint id = 0; DeviceContextApi.EnumDisplayDevices(device.DeviceName, id, ref display, 1); id++)
                    {
                        var isAttached = display.StateFlags.HasFlag(DisplayDeviceStateFlags.AttachedToDesktop);
                        if (!filterByValidity.HasValue)
                        {
                            yield return(DisplayDevice.FromDeviceInformation(this, device, display));
                        }
                        else if (filterByValidity.Value && isAttached)
                        {
                            if (!returned.ContainsKey(display.DeviceId) ||
                                !returned[display.DeviceId].Equals(display.DeviceKey))
                            {
                                returned.Add(display.DeviceId, display.DeviceKey);
                                yield return(DisplayDevice.FromDeviceInformation(this, device, display));

                                break;
                            }
                        }
                        else if (!filterByValidity.Value)
                        {
                            if (!isAttached)
                            {
                                if (!returned.ContainsKey(display.DeviceId) ||
                                    !returned[display.DeviceId].Equals(display.DeviceKey))
                                {
                                    displayDevice = DisplayDevice.FromDeviceInformation(this, device, display);
                                }
                            }
                            else
                            {
                                displayDevice = null;
                                break;
                            }
                        }
                        display = Native.DeviceContext.Structures.DisplayDevice.Initialize();
                    }
                    if (displayDevice != null)
                    {
                        returned.Add(displayDevice.DevicePath, displayDevice.DeviceKey);
                        yield return(displayDevice);
                    }
                }
                device = Native.DeviceContext.Structures.DisplayDevice.Initialize();
            }
        }
Exemplo n.º 2
0
        private static DeviceMode GetDeviceMode(DisplayDevice display, bool current)
        {
            var deviceMode = new DeviceMode(DeviceModeFields.None);

            return(!string.IsNullOrWhiteSpace(display.DisplayName) &&
                   DeviceContextApi.EnumDisplaySettings(display.DisplayName,
                                                        current ? DisplaySettingsMode.CurrentSettings : DisplaySettingsMode.RegistrySettings,
                                                        ref deviceMode)
                ? deviceMode
                : default);
Exemplo n.º 3
0
        internal IEnumerable <DisplayDevice> GetDisplayDevices(bool?filterByAvailability)
        {
            var returned = new Dictionary <string, string>();

            var adapterIndex = -1;

            while (true)
            {
                adapterIndex++;
                var adapter = Native.DeviceContext.Structures.DisplayDevice.Initialize();

                if (!DeviceContextApi.EnumDisplayDevices(null, (uint)adapterIndex, ref adapter, 0))
                {
                    break;
                }

                if (!DevicePath.Equals(adapter.DeviceId))
                {
                    continue;
                }

                var displayIndex = -1;

                while (true)
                {
                    displayIndex++;
                    var display = Native.DeviceContext.Structures.DisplayDevice.Initialize();

                    if (!DeviceContextApi.EnumDisplayDevices(adapter.DeviceName, (uint)displayIndex, ref display, 1))
                    {
                        break;
                    }

                    var displayDevice = DisplayDevice.FromDeviceInformation(this, adapter, display);

                    if (!filterByAvailability.HasValue)
                    {
                        yield return(displayDevice);
                    }
                    else if (displayDevice.IsAvailable == filterByAvailability.Value)
                    {
                        if (returned.ContainsKey(display.DeviceId) &&
                            returned[display.DeviceId].Equals(display.DeviceKey)
                            )
                        {
                            continue;
                        }

                        returned.Add(display.DeviceId, display.DeviceKey);

                        yield return(displayDevice);
                    }
                }
            }
        }
        internal MonitorCapabilities(DCHandle dcHandle)
        {
            _dcHandle = dcHandle;

            var tech = (DisplayTechnology)DeviceContextApi.GetDeviceCaps(_dcHandle, DeviceCapability.Technology);

            if (tech != DisplayTechnology.RasterDisplay)
            {
                throw new NotSupportedException();
            }
        }
Exemplo n.º 5
0
        private static DeviceMode GetDeviceMode(string screenName, DisplaySettingsMode flags)
        {
            var deviceMode = new DeviceMode(DeviceModeFields.None);

            return(!string.IsNullOrWhiteSpace(screenName) &&
                   DeviceContextApi.EnumDisplaySettings(
                       screenName,
                       flags,
                       ref deviceMode
                       )
                ? deviceMode
                : default);
Exemplo n.º 6
0
        /// <summary>
        ///     Applies settings that are saved using SaveDisplaySettings() or other similar methods but not yet applied
        /// </summary>
        public static void ApplySavedSettings()
        {
            var result = DeviceContextApi.ChangeDisplaySettingsEx(
                null,
                IntPtr.Zero,
                IntPtr.Zero,
                ChangeDisplaySettingsFlags.Reset,
                IntPtr.Zero);

            if (result != ChangeDisplaySettingsExResults.Successful)
            {
                throw new ModeChangeException($"[{result}]: Applying saved settings failed.", null, result);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Returns a list of all display adapters on this machine
        /// </summary>
        /// <returns>An enumerable list of DisplayAdapters</returns>
        public static IEnumerable <DisplayAdapter> GetDisplayAdapters()
        {
            var device    = Native.DeviceContext.Structures.DisplayDevice.Initialize();
            var deviceIds = new List <string>();

            for (uint i = 0; DeviceContextApi.EnumDisplayDevices(null, i, ref device, 0); i++)
            {
                if (!deviceIds.Contains(device.DeviceId))
                {
                    deviceIds.Add(device.DeviceId);
                    yield return(new DisplayAdapter(device.DeviceId, device.DeviceString, device.DeviceKey));
                }
                device = Native.DeviceContext.Structures.DisplayDevice.Initialize();
            }
        }
        /// <summary>
        ///     Returns a list of possible display setting for this display device
        /// </summary>
        /// <returns>An enumerable list of DisplayPossibleSettings</returns>
        public IEnumerable <DisplayPossibleSetting> GetPossibleSettings()
        {
            if (IsValid)
            {
                var deviceMode = new DeviceMode(DeviceModeFields.None);
                for (var i = 0;
                     DeviceContextApi.EnumDisplaySettings(DisplayName, (DisplaySettingsMode)i, ref deviceMode);
                     i++)
                {
                    yield return(new DisplayPossibleSetting(deviceMode));

                    deviceMode = new DeviceMode(DeviceModeFields.None);
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        ///     Gets a list of all active screens
        /// </summary>
        /// <returns>An array of <see cref="DisplayScreen" /> instances.</returns>
        public static DisplayScreen[] GetScreens()
        {
            var result   = new List <DisplayScreen>();
            var callback = new DeviceContextApi.MonitorEnumProcedure(
                (IntPtr handle, IntPtr dcHandle, ref RectangleL rect, IntPtr callbackObject) =>
            {
                result.Add(new DisplayScreen(handle));

                return(1);
            }
                );

            return(DeviceContextApi.EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, callback, IntPtr.Zero)
                ? result.ToArray()
                : null);
        }
Exemplo n.º 10
0
        internal void Save(DisplayDevice display, bool reset)
        {
            var deviceMode = GetDeviceMode(display);
            var flags      = ChangeDisplaySettingsFlags.UpdateRegistry | ChangeDisplaySettingsFlags.Global;

            flags |= reset ? ChangeDisplaySettingsFlags.Reset : ChangeDisplaySettingsFlags.NoReset;
            if (IsEnable && (Position.X == 0) && (Position.Y == 0))
            {
                flags |= ChangeDisplaySettingsFlags.SetPrimary;
            }
            var result = DeviceContextApi.ChangeDisplaySettingsEx(display.DisplayName, ref deviceMode, IntPtr.Zero,
                                                                  flags,
                                                                  IntPtr.Zero);

            if (result != ChangeDisplaySettingsExResults.Successful)
            {
                throw new ModeChangeException($"[{result}]: Applying saved settings failed.", display, result);
            }
        }
Exemplo n.º 11
0
        /// <summary>
        ///     Returns a list of possible display setting for this display device
        /// </summary>
        /// <returns>An enumerable list of DisplayPossibleSettings</returns>
        public IEnumerable <DisplayPossibleSetting> GetPossibleSettings()
        {
            if (!IsValid)
            {
                yield break;
            }

            var index = -1;

            while (true)
            {
                index++;
                var deviceMode = new DeviceMode(DeviceModeFields.None);
                if (!DeviceContextApi.EnumDisplaySettings(DisplayName, (DisplaySettingsMode)index, ref deviceMode))
                {
                    break;
                }
                yield return(new DisplayPossibleSetting(deviceMode));
            }
        }