Last active
July 23, 2023 22:09
-
-
Save peterfoot/223f271e5f3ae039e4d969c214f29226 to your computer and use it in GitHub Desktop.
Set the display brightness from a Win32 application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
} | |
} | |
} |
For laptops you can use:
https://github.com/mbahrami/LaptopBrightnessControl/blob/master/SetBrightness.cs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you kindly tell why this code does not work on laptops??? it works fine on desktops and all in one but it does not work on laptops.