Question

I am developing a Windows 8 application. Is there a way I can access the device's brightness settings so the user can adjust the brightness of the device from inside the app? Thanks

No correct solution

OTHER TIPS

I was using a batch file to change things but it also looks like Windows provided an API you can directly from C# (well p/Invoke and then call) as well... so all the above is still true and will still work, but you can also call

PowerGetActiveScheme

PowerWriteACValueIndex

PowerWriteDCValueIndex

Those links are nice too, because they have all the Sub guids listed out for you. So just P/Invoke those bad boys and call them directly from your app, no batch file needed :)

The built in windows Utility PowerCfg can change the brightness on the fly...

Typically PowerCfg is called from the cmd line or in batch files, but you can also call it from your app using Process.Start.

When using PowerCfg you needs to know several things:

  1. The scheme guid: which is the guid associated with the power scheme you want to change.
  2. The Sub Guid: Which is the guid associated with the power setting group you want to change.
  3. The Setting Guid: Which is the guid associated with the actual setting in that power settings group you want to change.
  4. The range or index value: Finally you need to know what value you want to change that setting to... Some settings have a range that you can set it to anything inside of that range, some settings have a list of values to choose and you need to know the index of the value you want.

The Scheme guid is actually the hardest one to get, since the user may have defined new schemes, and thus the guid needs to be found by calling PowerCfg -getactivescheme

The other guids are all constants and can be found by running PowerCfg - query

Once you have all the guids lined up you can set the ac (power plugged in) and dc (on battery) value for each setting. with:

POWERCFG -SETACVALUEINDEX <SCHEME_GUID> <SUB_GUID> <SETTING_GUID> <SettingIndex>
and
POWERCFG -SETDCVALUEINDEX <SCHEME_GUID> <SUB_GUID> <SETTING_GUID> <SettingIndex>

As an example... here is a little batch file that I use to turn off the Adaptive Brightness feature:

Echo Disable Adaptive Display Brightness Setting

for /f "tokens=2 delims=:" %%G in ('powercfg -getactivescheme') do set guid=%%G

for /f %%G in ("%guid%") do set guid=%%G

powercfg -setacvalueindex %guid% 7516b95f-f776-4464-8c53-06167f40cc99 fbd9aa66-9553-4097-ba44-ed6e9d65eab8 000
powercfg -setdcvalueindex %guid% 7516b95f-f776-4464-8c53-06167f40cc99 fbd9aa66-9553-4097-ba44-ed6e9d65eab8 000

The first couple of lines are getting the scheme guid, then the next two are setting the actual values

You could do something very similar for the Display Brightness settings... Which is this sub group and setting guid (same sub group as the Adaptive Brightness):

 Subgroup GUID: 7516b95f-f776-4464-8c53-06167f40cc99  (Display)
   Power Setting GUID: aded5e82-b909-4619-9949-f5d71dac0bcb  (Display brightness)
     Minimum Possible Setting: 0x00000000
     Maximum Possible Setting: 0x00000064
     Possible Settings increment: 0x00000001
     Possible Settings units: %

To call it from your C# app you could build a cmd file on the fly and run it with Process.Start

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top