问题
I have Windows Server 2008 installed on a Sony laptop and the brightness control doesn't work. I'd like to write a program to allow me to change it.
Currently what I have to do is open the Power control panel, click advanced settings, and fight through so many UAC boxes that anybody watching me must think I'm completely crazy.
I just want a simple little program to do it but i dont know what API to call
回答1:
I looked up John Rudy's link to WmiSetBrightness in MSDN and came up with this:
ManagementClass mclass = new ManagementClass("WmiMonitorBrightnessMethods");
mclass.Scope = new ManagementScope(@"\\.\root\wmi");
ManagementObjectCollection instances = mclass.GetInstances();
// I assume you get one instance per monitor
foreach(ManagementObject instance in instances)
{
ulong timeout = 1; // in seconds
ushort brightness = 50; // in percent
object[] args = new object[] { timeout, brightness };
instance.InvokeMethod("WmiSetBrightness", args);
}
Note: ManagementClass
, ManagementObjectCollection
, and ManagementObject
all implement IDisposable
. You should call Dispose()
or use "using
" to avoid leaking resources.
回答2:
This is vista only:
http://msdn.microsoft.com/en-us/library/ms775232.aspx
You need to identify the monitor with GetPhysicalMonitorsFromHMONITOR before setting its brightness with SetMonitorBrightness. I suspect nobody's done it before in .net so you'll probably need to write your own interop. The api doesn't appear to be particularly difficult so you should be able to do it pretty easily.
回答3:
As far as I'm aware, there are no managed (.NET) APIs for this. However, for Vista, there are unmanaged APIs available via interop. See MSDN: Monitor Configuration APIs and WmiSetBrightness.
There may be managed ways of hitting the WmiSetBrightness method, but I'm not aware of them.
If you're not on Vista or Server 2008, you're in for a world of unpleasantness: The software configuration would have to be done directly through the monitor driver. Based on your mentioning UAC above, I suspect this isn't the case for you, but the next guy might want to know. :)
回答4:
Just a brainstormer here... On the laptop you can change the brightness using some key-kombinations on the keyboard. It should be possible to send those keyspresses If you just want to make it darker/lighter and not have to set it to a specific value.
回答5:
To add to DaEagles answer, the http://www.pinvoke.net/ site can be a life saver when working with win32-apis in .Net. If the information does not exist there, please add it when you have made a succesful interop for .Net.
来源:https://stackoverflow.com/questions/373297/what-api-call-would-i-use-to-change-brightness-of-laptop-net