问题
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