美文网首页
C# 改变PC系统屏幕亮度的方法

C# 改变PC系统屏幕亮度的方法

作者: 逆风飞起的猪 | 来源:发表于2017-04-14 09:32 被阅读0次
//系统亮度
//适用于Unity3D PC端
 public class SysLight
 {
    [DllImport("gdi32.dll")]
    public static extern int GetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);
    RAMP ramp = new RAMP();
    [DllImport("gdi32.dll")]
    public static extern int SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);
    [DllImport("user32.dll")]
    static extern IntPtr GetDC(IntPtr hWnd);
    [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;
    }
    // 屏幕亮度方法
    // gamma 必须在3和44之间
    public void SetGamma(int gamma)
    {
        ramp.Red = new ushort[256];
        ramp.Green = new ushort[256];
        ramp.Blue = new ushort[256];
        for (int i = 1; i < 256; i++)
        {
            ramp.Red[i] = ramp.Green[i] = ramp.Blue[i] = 
            (ushort)(Math.Min(65535, Math.Max(0, Math.Pow((i + 1) / 256.0, gamma * 0.1) * 65535 + 0.5)));
        }
        SetDeviceGammaRamp(GetDC(IntPtr.Zero), ref ramp);
  }
}

相关文章

网友评论

      本文标题:C# 改变PC系统屏幕亮度的方法

      本文链接:https://www.haomeiwen.com/subject/jmqwattx.html