- using System;
- using System.Runtime.InteropServices;
-
- namespace ScreenBrightness
- {
-
- public class screenBrightness
- {
- //on importe les fonctions necessaires
- [DllImport("gdi32.dll",CharSet = CharSet.Auto)]
- public static extern bool SetDeviceGammaRamp(IntPtr hDc,
- [MarshalAs(UnmanagedType.LPArray)] ushort[,] lpRamp);
- [DllImport("user32.dll",CharSet = CharSet.Auto)]
- public static extern IntPtr GetDC(IntPtr hWnd);
-
- IntPtr screenDC;
-
- public screenBrightness(IntPtr hDC)
- {
- screenDC = hDC;
- }
-
- public bool setBrightness(int b)
- {
- IntPtr gammaDC;
- if(screenDC==IntPtr.Zero)
- {
- gammaDC = GetDC(IntPtr.Zero);
- } else gammaDC = screenDC;
-
- if(gammaDC == IntPtr.Zero) return false;
-
- ushort[,] gammaArray = new ushort[3,256];
-
- for (int i = 0; i < 256; i++)
- {
- int arrayValue = i * (b + 128);
-
- if (arrayValue > 65535)
- arrayValue = 65535;
-
- gammaArray[0, i] =
- gammaArray[1, i] =
- gammaArray[2, i] = (ushort)arrayValue;
-
- }
-
- return SetDeviceGammaRamp(gammaDC, gammaArray);
- }
- }
- }
using System;
using System.Runtime.InteropServices;
namespace ScreenBrightness
{
public class screenBrightness
{
//on importe les fonctions necessaires
[DllImport("gdi32.dll",CharSet = CharSet.Auto)]
public static extern bool SetDeviceGammaRamp(IntPtr hDc,
[MarshalAs(UnmanagedType.LPArray)] ushort[,] lpRamp);
[DllImport("user32.dll",CharSet = CharSet.Auto)]
public static extern IntPtr GetDC(IntPtr hWnd);
IntPtr screenDC;
public screenBrightness(IntPtr hDC)
{
screenDC = hDC;
}
public bool setBrightness(int b)
{
IntPtr gammaDC;
if(screenDC==IntPtr.Zero)
{
gammaDC = GetDC(IntPtr.Zero);
} else gammaDC = screenDC;
if(gammaDC == IntPtr.Zero) return false;
ushort[,] gammaArray = new ushort[3,256];
for (int i = 0; i < 256; i++)
{
int arrayValue = i * (b + 128);
if (arrayValue > 65535)
arrayValue = 65535;
gammaArray[0, i] =
gammaArray[1, i] =
gammaArray[2, i] = (ushort)arrayValue;
}
return SetDeviceGammaRamp(gammaDC, gammaArray);
}
}
}