C# 改變PC系統(tǒng)屏幕亮度的方法

//系統(tǒng)亮度
//適用于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);
  }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容