c# - 通过 NVIDIA 驱动程序以编程方式设置显示器亮度

标签 c# c++ .net pinvoke nvidia

我希望能够从 .NET 桌面应用程序更改显示器亮度。 (在 win7 和 nvidia gpu 上运行)

我找到了这个 winapi 函数: https://msdn.microsoft.com/en-us/library/windows/desktop/dd692972(v=vs.85).aspx

还有一些带有示例的 SO 问题,但是调用它对我没有任何作用。

但我发现我的 nvidia 控制面板允许使用 slider 调整亮度。

所以我想知道是否有 API 可以使用此功能?如果有人有一些关于如何访问它的示例代码?

最佳答案

我正在使用 AMD 卡运行 win7,以下示例对我有用。 SetBrightness 需要 0-100 范围内的参数。 我只有一台显示器要测试,所以我只为第一台设置了亮度。

using System;
using System.Runtime.InteropServices;

namespace SampleBrightness
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct PHYSICAL_MONITOR
    {
        public IntPtr hPhysicalMonitor;

        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string szPhysicalMonitorDescription;
    }

    public class BrightnessController : IDisposable
    {
        [DllImport("user32.dll", EntryPoint = "MonitorFromWindow")]
        public static extern IntPtr MonitorFromWindow([In] IntPtr hwnd, uint dwFlags);

        [DllImport("dxva2.dll", EntryPoint = "DestroyPhysicalMonitors")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DestroyPhysicalMonitors(uint dwPhysicalMonitorArraySize, ref PHYSICAL_MONITOR[] pPhysicalMonitorArray);

        [DllImport("dxva2.dll", EntryPoint = "GetNumberOfPhysicalMonitorsFromHMONITOR")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetNumberOfPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, ref uint pdwNumberOfPhysicalMonitors);

        [DllImport("dxva2.dll", EntryPoint = "GetPhysicalMonitorsFromHMONITOR")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, uint dwPhysicalMonitorArraySize, [Out] PHYSICAL_MONITOR[] pPhysicalMonitorArray);

        [DllImport("dxva2.dll", EntryPoint = "GetMonitorBrightness")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetMonitorBrightness(IntPtr handle, ref uint minimumBrightness, ref uint currentBrightness, ref uint maxBrightness);

        [DllImport("dxva2.dll", EntryPoint = "SetMonitorBrightness")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetMonitorBrightness(IntPtr handle, uint newBrightness);

        private uint _physicalMonitorsCount = 0;
        private PHYSICAL_MONITOR[] _physicalMonitorArray;

        private IntPtr _firstMonitorHandle;

        private uint _minValue = 0;
        private uint _maxValue = 0;
        private uint _currentValue = 0;

        public BrightnessController(IntPtr windowHandle)
        {
            uint dwFlags = 0u;
            IntPtr ptr = MonitorFromWindow(windowHandle, dwFlags);
            if (!GetNumberOfPhysicalMonitorsFromHMONITOR(ptr, ref _physicalMonitorsCount))
            {
                throw new Exception("Cannot get monitor count!");
            }
            _physicalMonitorArray = new PHYSICAL_MONITOR[_physicalMonitorsCount];

            if (!GetPhysicalMonitorsFromHMONITOR(ptr, _physicalMonitorsCount, _physicalMonitorArray))
            {
                throw new Exception("Cannot get phisical monitor handle!");
            }
            _firstMonitorHandle = _physicalMonitorArray[0].hPhysicalMonitor;

            if (!GetMonitorBrightness(_firstMonitorHandle, ref _minValue, ref _currentValue, ref _maxValue))
            {
                throw new Exception("Cannot get monitor brightness!");
            }
        }    

        public void SetBrightness(int newValue)
        {
            newValue = Math.Min(newValue, Math.Max(0, newValue));
            _currentValue = (_maxValue - _minValue) * (uint)newValue / 100u + _minValue;
            SetMonitorBrightness(_firstMonitorHandle, _currentValue);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_physicalMonitorsCount > 0)
                {
                    DestroyPhysicalMonitors(_physicalMonitorsCount, ref _physicalMonitorArray);
                }
            }
        }
    }
}

希望这对您有所帮助。

关于c# - 通过 NVIDIA 驱动程序以编程方式设置显示器亮度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31616148/

相关文章:

c# - "Cannot insert explicit value for identity column in table ' 电影 ' when IDENTITY_INSERT is set to OFF."

c# - 如何自定义WPF Ribbon 4.5(样式、模板等)

c# - 将 WinForm 应用程序限制为具有多个实例的一个进程

c++ - 多平台C++项目结构

c# - 我如何在 Vista 上设置 smtp 以便我可以使用 System.Net.Mail?

c# - 如何测试串行/ super 终端与 C# 的集成?

c# - 检测 IEnumerable<T> 是否已分组,我在检查类型时得到了一些疯狂的结果

c++ - 在 Turbo C++ 3.0 中读取文本文件

c++ - 是什么导致这个简单的 SDL 渲染循环中的帧速率卡顿?

.net - 如何测试 '-'是否存在,但字符 '['和 ']'之间不存在?