How to query GetMonitorBrightness from C#

你。 提交于 2019-12-18 06:53:49

问题


How does GetMonitorBrightness http://msdn.microsoft.com/en-us/library/ms775205.aspx work? Can someone give me an actual working implementation calling this code in C#?

I'm trying to retrieve the allowed brightness levels my laptop supports.

I have the following working code that sets the brightness from 1 to ~150. But I'm looking for the allowed input values (min max values).

    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;
                }
            }
        }
    }

回答1:


While using Interop should be possible this function is also available through WMI. Changing my original code a bit resulted in the following code that should work:

 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
        }
      }
    }
  }

Now I'm really curious how to handle 'special' cases like non laptop Screen's and if they implement any way to influence brightness.




回答2:


The function has an output of the minimum and maximum values:

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

This is assuming that you want the values for the first monitor.



来源:https://stackoverflow.com/questions/18083982/how-to-query-getmonitorbrightness-from-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!