How to query GetMonitorBrightness from C#

后端 未结 2 716
旧时难觅i
旧时难觅i 2020-12-19 14:57

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#?

相关标签:
2条回答
  • 2020-12-19 15:26

    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.

    0 讨论(0)
  • 2020-12-19 15:30

    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.

    0 讨论(0)
提交回复
热议问题