Beispiel #1
0
        public static void SetHDRState(bool enabled, string displayName = null)
        {
            uint pathCount, modeCount;

            var err = NativeMethods.GetDisplayConfigBufferSizes(QueryDisplayFlags.OnlyActivePaths, out pathCount, out modeCount);

            if (err == NativeConstants.ERROR_SUCCESS)
            {
                var pathsArray = new DisplayConfigPathInfo[pathCount];
                var modesArray = new DisplayConfigModeInfo[modeCount];

                DisplayConfigTopologyId displayTopology;

                err = NativeMethods.QueryDisplayConfig(QueryDisplayFlags.DatabaseCurrent, ref pathCount, pathsArray, ref modeCount, modesArray, out displayTopology);
                if (err == NativeConstants.ERROR_SUCCESS)
                {
                    foreach (var path in pathsArray)
                    {
                        // get display name
                        var info = new DISPLAYCONFIG_SOURCE_DEVICE_NAME();
                        info.header.type      = DISPLAYCONFIG_DEVICE_INFO_TYPE.DISPLAYCONFIG_DEVICE_INFO_GET_SOURCE_NAME;
                        info.header.size      = Marshal.SizeOf <DISPLAYCONFIG_SOURCE_DEVICE_NAME>();
                        info.header.adapterId = path.sourceInfo.adapterId;
                        info.header.id        = path.sourceInfo.id;

                        err = NativeMethods.DisplayConfigGetDeviceInfo(ref info);
                        if (err != NativeConstants.ERROR_SUCCESS)
                        {
                            break;
                        }

                        var deviceName = info.viewGdiDeviceName;
                        if (displayName != null && !EqualDisplayNames(deviceName, displayName))
                        {
                            continue;
                        }

                        var setpacket = new DISPLAYCONFIG_SET_ADVANCED_COLOR_INFO();
                        setpacket.header      = new DISPLAYCONFIG_DEVICE_INFO_HEADER();
                        setpacket.header.type = DISPLAYCONFIG_DEVICE_INFO_TYPE.DISPLAYCONFIG_DEVICE_INFO_SET_ADVANCED_COLOR_STATE;
                        setpacket.header.size = Marshal.SizeOf <DISPLAYCONFIG_SET_ADVANCED_COLOR_INFO>();;

                        var requestpacket = new DISPLAYCONFIG_GET_ADVANCED_COLOR_INFO();
                        requestpacket.header      = new DISPLAYCONFIG_DEVICE_INFO_HEADER();
                        requestpacket.header.type = DISPLAYCONFIG_DEVICE_INFO_TYPE.DISPLAYCONFIG_DEVICE_INFO_GET_ADVANCED_COLOR_INFO;
                        requestpacket.header.size = Marshal.SizeOf <DISPLAYCONFIG_GET_ADVANCED_COLOR_INFO>();;

                        for (int i = 0; i < modeCount; i++)
                        {
                            if (modesArray[i].infoType == DisplayConfigModeInfoType.Target)
                            {
                                setpacket.header.adapterId     = modesArray[i].adapterId;
                                setpacket.header.id            = modesArray[i].id;
                                requestpacket.header.adapterId = modesArray[i].adapterId;
                                requestpacket.header.id        = modesArray[i].id;

                                if (NativeMethods.DisplayConfigGetDeviceInfo(ref requestpacket) == NativeConstants.ERROR_SUCCESS)
                                {
                                    if (requestpacket.advancedColorSupported)
                                    {
                                        setpacket.enableAdvancedColor = enabled ? 1U : 0;
                                        NativeMethods.DisplayConfigSetDeviceInfo(ref setpacket);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (err != NativeConstants.ERROR_SUCCESS)
            {
                throw new Win32Exception(err);
            }
        }
Beispiel #2
0
 public static extern int DisplayConfigSetDeviceInfo(ref DISPLAYCONFIG_SET_ADVANCED_COLOR_INFO setPacket);