4

GetMonitorBrightness http://msdn.microsoft.com/en-us/library/ms775205.aspx是如何工作的?有人可以给我一个在 C# 中调用此代码的实际工作实现吗?

我正在尝试检索我的笔记本电脑支持的允许亮度级别。

我有以下工作代码将亮度设置为 1 到 ~150。但我正在寻找允许的输入值(最小最大值)。

    static void SetBrightness(byte targetBrightness)
    {
        ManagementScope scope = new ManagementScope("root\\WMI");
        SelectQuery query = new SelectQuery("WmiMonitorBrightnessMethods");
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
        {
            using (ManagementObjectCollection objectCollection = searcher.Get())
            {
                foreach (ManagementObject mObj in objectCollection)
                {
                    mObj.InvokeMethod("WmiSetBrightness",
                        new Object[] { UInt32.MaxValue, targetBrightness });
                    break;
                }
            }
        }
    }
4

2 回答 2

6

虽然应该可以使用 Interop,但该功能也可以通过 WMI 获得。稍微更改我的原始代码会导致以下代码应该可以工作:

 ManagementScope scope;
 SelectQuery query;

 scope = new ManagementScope("root\\WMI");
 query = new SelectQuery("SELECT * FROM WmiMonitorBrightness");

 using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
 {
    using (ManagementObjectCollection objectCollection = searcher.Get())
    {
      foreach (ManagementObject mObj in objectCollection)
      {
        Console.WriteLine(mObj.ClassPath);
        foreach (var item in mObj.Properties)
        {
          Console.WriteLine(item.Name + " " +item.Value.ToString());
          if(item.Name =="CurrentBrightness")
            //Do something with CurrentBrightness
        }
      }
    }
  }

现在我真的很好奇如何处理像非笔记本电脑屏幕这样的“特殊”情况,以及它们是否实施了任何影响亮度的方法。

于 2013-08-07T18:01:56.870 回答
1

该函数具有最小值和最大值的输出:

LPDWORD pdwMinimumBrightness=NULL;
LPDWORD pdwMaximumBrightness=NULL;
HANDLE pmh = pPhysicalMonitors[0].hPhysicalMonitor;
GetMonitorBrightness(pmh, pdwMinimumBrightness, pdwMaximumBrightness);

这是假设您想要第一个监视器的值。

于 2013-08-06T15:15:54.617 回答