1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
| using System; using System.Drawing; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms;
namespace ActualTransparent { public partial class TransparentForm : Form { private NotifyIcon trayIcon; private byte alpha = 160; private IntPtr targetHwnd = IntPtr.Zero; private bool isTransparent = false;
// 初始熱鍵 private Keys hotKeyMain = Keys.T; private HotKeyManager.Modifiers hotKeyModifier = HotKeyManager.Modifiers.Control | HotKeyManager.Modifiers.Alt;
// 控制元件 private TrackBar tbAlpha; private ComboBox modifierBox; private ComboBox keyBox; private Button btnSetHotKey; private Label lblLow; private Label lblHigh;
public TransparentForm() { // 設定窗體屬性 this.Text = "透明人間"; this.Size = new Size(400, 300); this.StartPosition = FormStartPosition.CenterScreen; this.TopMost = true; this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
// 系統托盤 trayIcon = new NotifyIcon { Icon = SystemIcons.Application, Visible = true, Text = "透明人間" };
var contextMenu = new ContextMenuStrip(); var exitItem = new ToolStripMenuItem("Exit"); exitItem.Click += (s, e) => { trayIcon.Visible = false; Application.Exit(); }; contextMenu.Items.Add(exitItem); trayIcon.ContextMenuStrip = contextMenu;
// 左鍵點擊顯示/隱藏窗體 trayIcon.MouseClick += (s, e) => { if (e.Button == MouseButtons.Left) { if (!this.Visible) { this.Show(); this.WindowState = FormWindowState.Normal; this.BringToFront(); } else { this.Hide(); } } };
// 建立控制元件 InitializeControls();
// 註冊全局熱鍵 HotKeyManager.RegisterHotKey(hotKeyMain, hotKeyModifier); Application.AddMessageFilter(new HotKeyMessageFilter(OnHotKeyPressed)); }
protected override void OnFormClosing(FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; // 取消關閉 this.Hide(); // 隱藏到托盤 } else { trayIcon.Visible = false; HotKeyManager.UnregisterAllHotKeys(); base.OnFormClosing(e); } }
private void InitializeControls() { // TrackBar tbAlpha = new TrackBar { Minimum = 50, Maximum = 255, Value = alpha, Orientation = Orientation.Vertical, TickFrequency = 10, Height = 180, Left = 50, Top = 40 }; tbAlpha.Scroll += (s, e) => alpha = (byte)tbAlpha.Value; this.Controls.Add(tbAlpha);
// Low / High 標籤 lblLow = new Label { Text = "低", Left = tbAlpha.Left - 40, Top = tbAlpha.Top + tbAlpha.Height - 15, Width = 40, TextAlign = ContentAlignment.MiddleRight }; lblHigh = new Label { Text = "高", Left = tbAlpha.Left - 40, Top = tbAlpha.Top - 5, Width = 40, TextAlign = ContentAlignment.MiddleRight }; this.Controls.Add(lblLow); this.Controls.Add(lblHigh);
// Modifier ComboBox modifierBox = new ComboBox { Left = 150, Top = 40, Width = 200 }; modifierBox.Items.AddRange(new string[] { "Control", "Alt", "Shift", "Win", "Ctrl+Alt", "Ctrl+Shift", "Alt+Shift" }); modifierBox.SelectedItem = "Ctrl+Alt"; this.Controls.Add(modifierBox);
// Key ComboBox 限制 0~9 與 A~Z keyBox = new ComboBox { Left = 150, Top = 80, Width = 200 }; for (char c = 'A'; c <= 'Z'; c++) keyBox.Items.Add((Keys)Enum.Parse(typeof(Keys), c.ToString())); keyBox.SelectedItem = Keys.T; this.Controls.Add(keyBox);
// 設定熱鍵按鈕 btnSetHotKey = new Button { Text = "設定熱鍵", Left = 150, Top = 120, Width = 200, Height = 40, Font = new Font("Segoe UI", 10, FontStyle.Bold) }; btnSetHotKey.Click += (s, e) => { HotKeyManager.UnregisterAllHotKeys(); hotKeyMain = (Keys)keyBox.SelectedItem; hotKeyModifier = ParseModifiers(modifierBox.SelectedItem.ToString()); HotKeyManager.RegisterHotKey(hotKeyMain, hotKeyModifier); }; this.Controls.Add(btnSetHotKey); }
private HotKeyManager.Modifiers ParseModifiers(string text) { HotKeyManager.Modifiers mods = 0; if (text.Contains("Control") || text.Contains("Ctrl")) mods |= HotKeyManager.Modifiers.Control; if (text.Contains("Alt")) mods |= HotKeyManager.Modifiers.Alt; if (text.Contains("Shift")) mods |= HotKeyManager.Modifiers.Shift; if (text.Contains("Win")) mods |= HotKeyManager.Modifiers.Win; return mods; }
private void OnHotKeyPressed() { Win32.GetCursorPos(out Point pt); IntPtr hwnd = Win32.WindowFromPoint(pt);
if (hwnd == IntPtr.Zero || hwnd == this.Handle || IsSystemWindow(hwnd)) return;
if (!isTransparent) { SetAlphaRecursive(hwnd, alpha); targetHwnd = hwnd; isTransparent = true; } else { if (targetHwnd != IntPtr.Zero) SetAlphaRecursive(targetHwnd, 255); isTransparent = false; } }
// 遞迴設透明 private void SetAlphaRecursive(IntPtr hwnd, byte a) { SetAlpha(hwnd, a); IntPtr child = Win32.GetWindow(hwnd, Win32.GW_CHILD); while (child != IntPtr.Zero) { SetAlphaRecursive(child, a); child = Win32.GetWindow(child, Win32.GW_HWNDNEXT); } }
private void SetAlpha(IntPtr hwnd, byte a) { int style = Win32.GetWindowLong(hwnd, Win32.GWL_EXSTYLE); if ((style & Win32.WS_EX_LAYERED) == 0) Win32.SetWindowLong(hwnd, Win32.GWL_EXSTYLE, style | Win32.WS_EX_LAYERED);
Win32.SetLayeredWindowAttributes(hwnd, 0, a, Win32.LWA_ALPHA); }
private bool IsSystemWindow(IntPtr hwnd) { if (hwnd == IntPtr.Zero) return true;
IntPtr root = Win32.GetAncestor(hwnd, 3); // GA_ROOTOWNER string className = GetClassName(root); if (string.IsNullOrEmpty(className)) return true;
string[] systemClasses = { "Shell_TrayWnd", "DV2ControlHost", "Windows.UI.Core.CoreWindow" }; foreach (var sys in systemClasses) if (className == sys) return true;
IntPtr taskbar = Win32.FindWindow("Shell_TrayWnd", null); if (taskbar != IntPtr.Zero && Win32.IsChild(taskbar, hwnd)) return true;
if (root == this.Handle) return true;
return false; }
private string GetClassName(IntPtr hwnd) { StringBuilder sb = new StringBuilder(256); int ret = Win32.GetClassName(hwnd, sb, sb.Capacity); if (ret > 0) return sb.ToString(); return ""; } }
public class HotKeyMessageFilter : IMessageFilter { private const int WM_HOTKEY = 0x0312; private Action callback; public HotKeyMessageFilter(Action callback) { this.callback = callback; } public bool PreFilterMessage(ref Message m) { if (m.Msg == WM_HOTKEY) { callback?.Invoke(); return true; } return false; } }
public static class HotKeyManager { public enum Modifiers : uint { Alt = 1, Control = 2, Shift = 4, Win = 8 } private static int idCounter = 0;
[DllImport("user32.dll")] private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")] private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public static void RegisterHotKey(Keys key, Modifiers mods) { idCounter++; RegisterHotKey(IntPtr.Zero, idCounter, (uint)mods, (uint)key); }
public static void UnregisterAllHotKeys() { for (int i = 1; i <= idCounter; i++) UnregisterHotKey(IntPtr.Zero, i); idCounter = 0; } }
public static class Win32 { public const int GWL_EXSTYLE = -20; public const int WS_EX_LAYERED = 0x80000; public const int LWA_ALPHA = 0x2; public const uint GA_ROOT = 2; public const uint GA_ROOTOWNER = 3; public const uint GW_CHILD = 5; public const uint GW_HWNDNEXT = 2;
[DllImport("user32.dll")] public static extern IntPtr WindowFromPoint(Point p);
[DllImport("user32.dll")] public static extern bool GetCursorPos(out Point lpPoint);
[DllImport("user32.dll")] public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")] public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")] public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll")] public static extern IntPtr GetAncestor(IntPtr hwnd, uint gaFlags);
[DllImport("user32.dll")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")] public static extern bool IsChild(IntPtr hWndParent, IntPtr hWnd);
[DllImport("user32.dll")] public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll")] public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd); } }
|