夜間模式在手機上的應用很廣泛,很多手機應用都添加了夜間模式的主題,但是在電腦上卻很少有這樣保護眼睛的主題,很多時候屏幕的背景顏色都是白色的,在晚上顯得特別刺眼,如果可以調節屏幕光線的亮度的話,就可以讓屏幕變暗,在一定程度上可以保護眼睛,但是在顯示器上直接調節屏幕亮度顯得特別麻煩,而Windows本身沒有提供一些關於亮度的設置,也沒有提供有關於顯示器的一些編程接口,所以這方面的應用顯得非常少,下面通過透明窗口,讓屏幕加上一層蒙版達到調節屏幕明亮的目的,這個API找了好久才找到
代碼比較簡單,注釋都在代碼上,直接上代碼
public partial class MaskForm : Form { /* * 下面這段代碼主要用來調用Windows API實現窗體透明(鼠標可以穿透窗體) */ [DllImport("user32.dll", EntryPoint = "GetWindowLong")] public static extern long GetWindowLong(IntPtr hwnd, int nIndex); [DllImport("user32.dll", EntryPoint = "SetWindowLong")] public static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong); [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")] private static extern int SetLayeredWindowAttributes(IntPtr Handle, int crKey, byte bAlpha, int dwFlags); const int GWL_EXSTYLE = -20; const int WS_EX_TRANSPARENT = 0x20; const int WS_EX_LAYERED = 0x80000; const int LWA_ALPHA = 2; public MaskForm() { InitializeComponent(); } private void MaskForm_Load(object sender, EventArgs e) { // 取消窗體任務欄 ShowInTaskbar = false; // 窗體位於Windows最頂部 this.TopMost = true; // 去除窗體邊框 this.FormBorderStyle = FormBorderStyle.None;//5+1+a+s+p+x // 設置窗體最大化大小(除底部任務欄部分) this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height); // 設置Windows窗口狀態為最大化模式 this.WindowState = FormWindowState.Maximized; // 設置Windows屬性 SetWindowLong(this.Handle, GWL_EXSTYLE, GetWindowLong(this.Handle, GWL_EXSTYLE) | WS_EX_TRANSPARENT | WS_EX_LAYERED); SetLayeredWindowAttributes(this.Handle, 0, 100, LWA_ALPHA); this.BackColor = Color.Black; } public void SetOpacity(byte opacity) { SetLayeredWindowAttributes(this.Handle, 0, opacity, LWA_ALPHA); } }
當打開該窗口時,屏幕就會變暗
代碼(vs2012)
http://files.cnblogs.com/bomo/%E5%A4%9C%E9%97%B4%E6%A8%A1%E5%BC%8F.zip