using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; namespace ryCommon { /// /// 系统函数 /// public class RySystem { /// /// /// [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; } /// /// 获取屏幕伽马值 /// /// /// /// [DllImport("gdi32.dll", SetLastError = true)] public static extern bool GetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp); /// /// 设置屏幕伽马值 /// /// /// /// [DllImport("gdi32.dll", SetLastError = true)] public static extern bool SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp); private static readonly double[] _gammas = { 1, 1, 1 }; private static short _brightness = 100; // 创建结构体用于返回捕获时间 [StructLayout(LayoutKind.Sequential)] struct LASTINPUTINFO { // 设置结构体块容量 [MarshalAs(UnmanagedType.U4)] public int cbSize; // 捕获的时间 [MarshalAs(UnmanagedType.U4)] public uint dwTime; } [DllImport("user32.dll")] private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); /// /// 获取键盘和鼠标没有操作的时间 /// /// public static long GetLastInputTime() { LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO(); vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo); // 捕获时间 if (!GetLastInputInfo(ref vLastInputInfo)) return 0; else return Environment.TickCount - (long)vLastInputInfo.dwTime; } /// /// 设置屏幕 Gamma值 /// /// /// /// /// public static bool SetGamma(double gammaRed, double gammaGreen, double gammaBlue) { _gammas[0] = gammaRed; _gammas[1] = gammaGreen; _gammas[2] = gammaBlue; return UpdateRamps(); } /// /// 设置屏幕亮度 /// /// /// public static bool SetBrightness(short brightness) { if (brightness > 100) brightness = 100; if (brightness < 0) brightness = 0; _brightness = brightness; return UpdateRamps(); } private static bool UpdateRamps() { double brightness = (double)_brightness / 100f; RAMP ramp = new RAMP { Red = new UInt16[256], Green = new UInt16[256], Blue = new UInt16[256] }; for (int i = 0; i < 256; i++) { ramp.Red[i] = (ushort)GetGamma(_gammas[0]); ramp.Green[i] = (ushort)GetGamma(_gammas[1]); ramp.Blue[i] = (ushort)GetGamma(_gammas[2]); double GetGamma(double gamma) { var r = (Math.Pow(i / 256.0, 1.0 / gamma) * 65535 + 0.5); r *= brightness; if (r > 65535) r = 65535; if (r < 0) r = 0; return r; } } var retVal = SetDeviceGammaRamp(Graphics.FromHwnd(IntPtr.Zero).GetHdc(), ref ramp); //Memory allocated through stackalloc is automatically free'd //by the CLR. return retVal; } /// /// 获取系统 /// /// public static string GetOSystemVer() { switch (System.Environment.OSVersion.Version.Major + "." + System.Environment.OSVersion.Version.Minor) { case "5.0": return "Windows2000"; case "5.1": return "WindowsXP"; case "5.2": return "Windows2003"; case "6.0": return "Windows2008"; case "6.1": return "Windows7"; case "6.2": return "Windows8 Or Windows8.1"; case "6.3": return "Windows8.1"; case "10.0": return "Windows10"; default: return "未知"; } } } }