如何从C#查询GetMonitorBrightness [英] How to query GetMonitorBrightness from C#
问题描述
GetMonitorBrightness如何 http://msdn.microsoft.com/en-us /library/ms775205.aspx 工作吗?有人可以给我一个实际可行的实现,用C#调用此代码吗?
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.
我有以下工作代码将亮度设置为1至〜150.但是我正在寻找允许的输入值(最小最大值).
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;
}
}
}
}
推荐答案
虽然应该可以使用Interop,但也可以通过WMI使用此功能.稍微更改我的原始代码会导致以下代码正常工作:
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.
这篇关于如何从C#查询GetMonitorBrightness的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!