I have an integrated jetway screen which I want to change the brightness of from a WinForms Application. From my research online I found 2 (and a half) ways of changing the brightness:
The first is using the Windows WMI interface to change the brightess and the second is to change the driver from external physical monitor settings. In order to try these two I have made the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Diagnostics;
using System.Management;
using System.ServiceProcess;
namespace ConsoleApp38
{
internal class Program
{
public static class WindowsSettingsBrightnessController
{
public static int Get()
{
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 0;
}
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);
}
}
}
public class PhysicalMonitorBrightnessController : IDisposable
{
#region DllImport
[DllImport("dxva2.dll", EntryPoint = "GetNumberOfPhysicalMonitorsFromHMONITOR")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetNumberOfPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, ref uint pdwNumberOfPhysicalMonitors);
[DllImport("dxva2.dll", EntryPoint = "GetPhysicalMonitorsFromHMONITOR")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetPhysicalMonitorsFromHMONITOR(IntPtr hMonitor, uint dwPhysicalMonitorArraySize, [Out] PHYSICAL_MONITOR[] pPhysicalMonitorArray);
[DllImport("dxva2.dll", EntryPoint = "GetMonitorBrightness")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetMonitorBrightness(IntPtr handle, ref uint minimumBrightness, ref uint currentBrightness, ref uint maxBrightness);
[DllImport("dxva2.dll", EntryPoint = "SetMonitorBrightness")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetMonitorBrightness(IntPtr handle, uint newBrightness);
[DllImport("dxva2.dll", EntryPoint = "DestroyPhysicalMonitor")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DestroyPhysicalMonitor(IntPtr hMonitor);
[DllImport("dxva2.dll", EntryPoint = "DestroyPhysicalMonitors")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DestroyPhysicalMonitors(uint dwPhysicalMonitorArraySize, [In] PHYSICAL_MONITOR[] pPhysicalMonitorArray);
[DllImport("user32.dll")]
static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, EnumMonitorsDelegate lpfnEnum, IntPtr dwData);
delegate bool EnumMonitorsDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData);
#endregion
private IReadOnlyCollection<MonitorInfo> Monitors { get; set; }
public PhysicalMonitorBrightnessController()
{
UpdateMonitors();
}
#region Get & Set
public void Set(uint brightness)
{
Set(brightness, true);
}
private void Set(uint brightness, bool refreshMonitorsIfNeeded)
{
bool isSomeFail = false;
foreach (var monitor in Monitors)
{
uint realNewValue = (monitor.MaxValue - monitor.MinValue) * brightness / 100 + monitor.MinValue;
if (SetMonitorBrightness(monitor.Handle, realNewValue))
{
monitor.CurrentValue = realNewValue;
}
else if (refreshMonitorsIfNeeded)
{
isSomeFail = true;
break;
}
}
if (refreshMonitorsIfNeeded && (isSomeFail || !Monitors.Any()))
{
UpdateMonitors();
Set(brightness, false);
return;
}
}
public int Get()
{
if (!Monitors.Any())
{
return -1;
}
return (int)Monitors.Average(d => d.CurrentValue);
}
#endregion
private void UpdateMonitors()
{
DisposeMonitors(this.Monitors);
var monitors = new List<MonitorInfo>();
EnumDisplayMonitors(IntPtr.Zero, IntPtr.Zero, (IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData) =>
{
uint physicalMonitorsCount = 0;
if (!GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, ref physicalMonitorsCount))
{
// Cannot get monitor count
return true;
}
var physicalMonitors = new PHYSICAL_MONITOR[physicalMonitorsCount];
if (!GetPhysicalMonitorsFromHMONITOR(hMonitor, physicalMonitorsCount, physicalMonitors))
{
// Cannot get physical monitor handle
return true;
}
foreach (PHYSICAL_MONITOR physicalMonitor in physicalMonitors)
{
uint minValue = 0, currentValue = 0, maxValue = 0;
if (!GetMonitorBrightness(physicalMonitor.hPhysicalMonitor, ref minValue, ref currentValue, ref maxValue))
{
DestroyPhysicalMonitor(physicalMonitor.hPhysicalMonitor);
continue;
}
var info = new MonitorInfo
{
Handle = physicalMonitor.hPhysicalMonitor,
MinValue = minValue,
CurrentValue = currentValue,
MaxValue = maxValue,
};
monitors.Add(info);
}
return true;
}, IntPtr.Zero);
this.Monitors = monitors;
}
public void Dispose()
{
DisposeMonitors(Monitors);
GC.SuppressFinalize(this);
}
private static void DisposeMonitors(IEnumerable<MonitorInfo> monitors)
{
if (monitors?.Any() == true)
{
PHYSICAL_MONITOR[] monitorArray = monitors.Select(m => new PHYSICAL_MONITOR { hPhysicalMonitor = m.Handle }).ToArray();
DestroyPhysicalMonitors((uint)monitorArray.Length, monitorArray);
}
}
#region Classes
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct PHYSICAL_MONITOR
{
public IntPtr hPhysicalMonitor;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szPhysicalMonitorDescription;
}
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
}
public class MonitorInfo
{
public uint MinValue { get; set; }
public uint MaxValue { get; set; }
public IntPtr Handle { get; set; }
public uint CurrentValue { get; set; }
}
#endregion
}
static void Main(string[] args)
{
PhysicalMonitorBrightnessController brightnessController = new PhysicalMonitorBrightnessController();
Console.WriteLine(brightnessController.Get());
brightnessController.Set(10);
// Console.WriteLine(WindowsSettingsBrightnessController.Get());
// WindowsSettingsBrightnessController.Set(10);
}
}
}
I tried both parts of the code on my laptop, my desktop, and the integrated screen. In the laptop, WMI worked flawlessly while the other did not (get brightness always returned -1 suggesting that there aren't any external monitors found). On the desktop, the physical monitor worked flawlessly while the other did not (various errors such as "Not Found" or "Not Supported"). Either way didn't work on the jetway internal screen. I had to thus settle with the third option which was changing the gamma settings.
The code I have written for the Gamma changing was as follows:
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("gdi32.dll")]
public static extern bool SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct RAMP
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public UInt16[] Red;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public UInt16[] Green;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public UInt16[] Blue;
}
public static void SetGamma(int gamma)
{
if (gamma <= 256 && gamma >= 1)
{
RAMP ramp = new RAMP();
ramp.Red = new ushort[256];
ramp.Green = new ushort[256];
ramp.Blue = new ushort[256];
for (int i = 1; i < 256; i++)
{
int iArrayValue = i * (gamma + 128);
if (iArrayValue > 65535)
{
iArrayValue = 65535;
}
ramp.Red[i] = ramp.Blue[i] = ramp.Green[i] = (ushort)iArrayValue;
}
SetDeviceGammaRamp(GetDC(IntPtr.Zero), ref ramp);
}
}
Although the gamma function worked, I was not satisfied with the results in any way and I am looking for a way to change the actual brightness of the screen instead of Gamma.
Additionally, the Windows installed on the system doesn't have a brightness-changing setting and currently, the only way I found that can change brightness is the Intel graphics application.
All help is appreciated.