EyesBaby 專案核心功能之一是全螢幕前景色彩調節器:覆蓋整個 Windows 桌面區域的透明視窗,使用者可調整顏色與透明度以減輕眼部疲勞。關鍵技術難點在於滑鼠點擊穿透——視窗必須可見但不攔截任何輸入事件。
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace EyesBaby
{
public partial class WinScreenAdjust : Form
{
// Win32 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; // 使用 Alpha 通道
public WinScreenAdjust()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 1. 基本視窗屬性設定
ShowInTaskbar = false; // 不顯示在工作列
TopMost = true; // 永遠置頂
FormBorderStyle = FormBorderStyle.None; // 無邊框
// 2. 尺寸覆蓋工作區(排除工作列)
MaximumSize = new Size(
Screen.PrimaryScreen.WorkingArea.Width,
Screen.PrimaryScreen.WorkingArea.Height);
WindowState = FormWindowState.Maximized;
// 3. 關鍵:修改擴充視窗樣式
// 取得現有樣式 | 加入 WS_EX_TRANSPARENT | WS_EX_LAYERED
long exStyle = GetWindowLong(Handle, GWL_EXSTYLE);
SetWindowLong(Handle, GWL_EXSTYLE, exStyle | WS_EX_TRANSPARENT | WS_EX_LAYERED);
// 4. 設定分層視窗透明度 (0-255, 128=50%)
SetLayeredWindowAttributes(Handle, 0, 128, LWA_ALPHA);
}
}
}
SetLayeredWindowAttributes 設定透明度LWA_ALPHA 參數無效SetLayeredWindowAttributes(hwnd, crKey, bAlpha, dwFlags)
// hwnd: 視窗控制代碼
// crKey: 透明色鍵 (配合 LWA_COLORKEY 使用),此處設 0 不使用
// bAlpha: Alpha 值 (0=全透明, 255=不透明),此處 128 為 50%
// dwFlags: LWA_ALPHA (2) 表示使用 bAlpha 參數
// 開啟色彩選擇對話框
if (colorDialog.ShowDialog() == DialogResult.OK)
{
// 直接套用至視窗背景色
this.BackColor = colorDialog.Color;
// 儲存至設定檔 (以色彩名稱/ARGB 值)
ConfigHelper.WinForeColor = this.BackColor.Name; // 或 ToArgb().ToString()
}
色彩與透明度疊加效果:視窗背景色提供色調,Alpha 值控制疊加強度,達成「護眼模式」視覺效果。
┌─────────────────────────────────────┐ │ Windows 桌面 │ ├─────────────────────────────────────┤ │ WinScreenAdjust (TopMost, 最大化) │ ◄── WS_EX_LAYERED + Alpha=128 │ BackColor = 使用者選擇色彩 │ ◄── 色調來源 │ WS_EX_TRANSPARENT = true │ ◄── 滑鼠穿透關鍵 ├─────────────────────────────────────┤ │ 其他應用程式視窗 │ ◄── 滑鼠事件正常傳遞至此 └─────────────────────────────────────┘
Screen.AllScreens,建立多個視窗分別覆蓋各螢幕工作區Windows.UI.Composition 或 Magnification API 實現更高效能的系統級色彩濾鏡此技術不僅適用於護眼軟體,亦可延伸至螢幕錄製遮罩、全螢幕水印、教學示範高亮等場景,掌握 WS_EX_TRANSPARENT 與分層視窗組合技巧,是 Windows 桌面開發進階必備技能。