Skip to content

Instantly share code, notes, and snippets.

@peterfoot
Last active July 23, 2023 22:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save peterfoot/223f271e5f3ae039e4d969c214f29226 to your computer and use it in GitHub Desktop.
Save peterfoot/223f271e5f3ae039e4d969c214f29226 to your computer and use it in GitHub Desktop.
Set the display brightness from a Win32 application
public static class BrightnessHelper
{
public static void SetBrightness(uint brightness)
{
if (brightness > 100)
{
throw new ArgumentOutOfRangeException("brightness");
}
// get handle to primary display
IntPtr hmon = NativeMethods.MonitorFromWindow(IntPtr.Zero, NativeMethods.MONITOR_DEFAULTTO.PRIMARY);
// get number of physical displays (assume only one for simplicity)
int num;
bool success = NativeMethods.GetNumberOfPhysicalMonitorsFromHMONITOR(hmon, out num);
NativeMethods.PHYSICAL_MONITOR pmon = new NativeMethods.PHYSICAL_MONITOR();
success = NativeMethods.GetPhysicalMonitorsFromHMONITOR(hmon, num, ref pmon);
uint min, max, current;
// commonly min and max are 0-100 which represents a percentage brightness
success = NativeMethods.GetMonitorBrightness(pmon.hPhysicalMonitor, out min, out current, out max);
// set to full brightness
success = NativeMethods.SetMonitorBrightness(pmon.hPhysicalMonitor, brightness);
success = NativeMethods.DestroyPhysicalMonitors(num, ref pmon);
}
private static class NativeMethods
{
[DllImport("Dxva2")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetMonitorBrightness(IntPtr hMonitor,
out uint pdwMinimumBrightness,
out uint pdwCurrentBrightness,
out uint pdwMaximumBrightness);
[DllImport("Dxva2")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetMonitorBrightness(IntPtr hMonitor, uint newBrightness);
[DllImport("Dxva2")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetNumberOfPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, out int numberOfPhysicalMonitors);
[DllImport("Dxva2", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, int physicalMonitorArraySize, ref PHYSICAL_MONITOR physicalMonitorArray);
[DllImport("Dxva2", CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DestroyPhysicalMonitors(int physicalMonitorArraySize, ref PHYSICAL_MONITOR physicalMonitorArray);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct PHYSICAL_MONITOR
{
internal IntPtr hPhysicalMonitor;
//[PHYSICAL_MONITOR_DESCRIPTION_SIZE]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
internal string szPhysicalMonitorDescription;
}
[DllImport("User32")]
internal static extern IntPtr MonitorFromWindow(IntPtr hwnd, MONITOR_DEFAULTTO dwFlags);
internal enum MONITOR_DEFAULTTO
{
NULL = 0x00000000,
PRIMARY = 0x00000001,
NEAREST = 0x00000002,
}
}
}
@mbahrami
Copy link

mbahrami commented Nov 6, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment