Home › Forums › Software Development › Keep eyex on while screen is off › Reply To: Keep eyex on while screen is off
05/12/2014 at 07:12 #2159
Gal Sont
Participant
Hi,
These changes would be most welcomed 🙂
Thank you for your idea, this is currently what I do + reducing the brightness to minimum. This solution is 80% good as the screen is still quite bright in a dark room. I like perfection 🙂
In the meanwhile I reached the right person in Microsoft and will update here on any progress.
Here is the code for turning the screen on / off and setting brightness and gamma to minimum (brightness works only on tablets / laptops so use gamma for pc):
usage – TurnScreenOnOff(true);
[DllImport("user32.dll")]
private static extern int SendMessage(uint hWnd, int hMsg, int wParam, int lParam);
private static byte originalBritness;
private static RAMP originalGamma;
private static int WM_SYSCOMMAND = 0x0112;
private static int SC_MONITORPOWER = 0xF170; //Using the system pre-defined MSDN constants that can be used by the SendMessage() function .
private static uint HWND_BROADCAST = 0xffff;
public enum MonitorState
{
MonitorStateOn = -1,
MonitorStateOff = 2,
MonitorStateStandBy = 1
}
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("gdi32.dll")]
public static extern bool SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);
[DllImport("gdi32.dll")]
static extern bool GetDeviceGammaRamp(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);
}
}
private static void SetBrightness(byte targetBrightness)
{
ManagementScope scope = new ManagementScope(@"root\WMI");
SelectQuery query = new SelectQuery("WmiMonitorBrightnessMethods");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection objects = searcher.Get();
foreach (ManagementObject obj2 in objects)
{
obj2.InvokeMethod("WmiSetBrightness", new object[] { uint.MaxValue, targetBrightness });
break;
}
objects.Dispose();
searcher.Dispose();
}
private static byte GetBrightness()
{
ManagementScope scope = new ManagementScope(@"root\WMI");
SelectQuery query = new SelectQuery("WmiMonitorBrightness");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection objects = searcher.Get();
byte propertyValue = 0;
foreach (ManagementObject obj2 in objects)
{
propertyValue = (byte)obj2.GetPropertyValue("CurrentBrightness");
break;
}
objects.Dispose();
searcher.Dispose();
return propertyValue;
}
public static void TurnScreenOnOff(bool turnOff)
{
if (turnOff)
{
//originalBritness = GetBrightness();
//GetDeviceGammaRamp(GetDC(IntPtr.Zero), ref originalGamma);
//SetBrightness(0);
//SetGamma(1);
//blankBlackWindow.Show();
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (int)MonitorState.MonitorStateStandBy);
}
else
{
//SetBrightness(originalBritness);
//SetDeviceGammaRamp(GetDC(IntPtr.Zero), ref originalGamma);
//blankBlackWindow.Close();
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (int)MonitorState.MonitorStateOn);
}
}