2022-11-14 11:06:03 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Management;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PowerControl.Helpers
|
|
|
|
|
|
{
|
2022-11-20 15:12:12 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Taken from: https://gist.github.com/maxkoshevoi/b8a1ad91f4d2a9fd3931168c14080694
|
|
|
|
|
|
/// </summary>
|
2022-11-14 11:06:03 +01:00
|
|
|
|
public static class WindowsSettingsBrightnessController
|
|
|
|
|
|
{
|
2022-11-24 22:37:24 +01:00
|
|
|
|
public static void Increase(int brightness)
|
|
|
|
|
|
{
|
|
|
|
|
|
var current = Get();
|
|
|
|
|
|
current += brightness;
|
|
|
|
|
|
current = Math.Clamp(current, 0, 100);
|
|
|
|
|
|
Set(current);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-20 15:12:12 +01:00
|
|
|
|
public static int Get(double roundValue = 10.0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (int)(Math.Round(Get() / roundValue) * roundValue);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-14 11:06:03 +01:00
|
|
|
|
public static int Get()
|
|
|
|
|
|
{
|
2022-11-19 21:28:28 +01:00
|
|
|
|
try
|
2022-11-14 11:06:03 +01:00
|
|
|
|
{
|
2022-11-19 21:28:28 +01:00
|
|
|
|
using var mclass = new ManagementClass("WmiMonitorBrightness")
|
|
|
|
|
|
{
|
|
|
|
|
|
Scope = new ManagementScope(@"\\.\root\wmi")
|
|
|
|
|
|
};
|
|
|
|
|
|
using var instances = mclass.GetInstances();
|
|
|
|
|
|
foreach (ManagementObject instance in instances)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (byte)instance.GetPropertyValue("CurrentBrightness");
|
|
|
|
|
|
}
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
2022-11-14 11:06:03 +01:00
|
|
|
|
{
|
2022-11-19 21:28:28 +01:00
|
|
|
|
return -1;
|
2022-11-14 11:06:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void Set(int brightness)
|
|
|
|
|
|
{
|
|
|
|
|
|
using var mclass = new ManagementClass("WmiMonitorBrightnessMethods")
|
|
|
|
|
|
{
|
|
|
|
|
|
Scope = new ManagementScope(@"\\.\root\wmi")
|
|
|
|
|
|
};
|
|
|
|
|
|
using var instances = mclass.GetInstances();
|
|
|
|
|
|
var args = new object[] { 1, brightness };
|
|
|
|
|
|
foreach (ManagementObject instance in instances)
|
|
|
|
|
|
{
|
|
|
|
|
|
instance.InvokeMethod("WmiSetBrightness", args);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|