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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
using System; using System.Runtime.InteropServices;
internal static class Program { private const string WindowClassName = "TrayNativeExperimentWindowClass"; private const string Tooltip = "Tray Native Experiment";
private const int WM_DESTROY = 0x0002; private const int WM_COMMAND = 0x0111; private const int WM_USER = 0x0400; private const int WM_NULL = 0x0000;
private const int WM_LBUTTONUP = 0x0202; private const int WM_RBUTTONUP = 0x0205;
private const int NIF_MESSAGE = 0x00000001; private const int NIF_ICON = 0x00000002; private const int NIF_TIP = 0x00000004;
private const int NIM_ADD = 0x00000000; private const int NIM_DELETE = 0x00000002; private const int NIM_SETVERSION = 0x00000004;
private const int NOTIFYICON_VERSION_4 = 4;
private const uint TPM_LEFTALIGN = 0x0000; private const uint TPM_BOTTOMALIGN = 0x0020; private const uint TPM_RIGHTBUTTON = 0x0002; private const uint TPM_RETURNCMD = 0x0100;
private const uint MF_STRING = 0x0000; private const uint MF_SEPARATOR = 0x0800;
private const int MenuCmdShow = 1001; private const int MenuCmdExit = 1002;
private static readonly uint TrayCallbackMessage = WM_USER + 1;
private static IntPtr _hwnd; private static IntPtr _menu; private static bool _running = true;
private static WndProcDelegate? _wndProcDelegate;
public static int Main() { Console.WriteLine("Initializing app-level dark mode..."); TryEnableAppDarkMode();
_wndProcDelegate = WndProc;
ushort atom = RegisterWindowClass(); if (atom == 0) { Console.WriteLine("RegisterClassEx failed."); return 1; }
_hwnd = CreateNativeWindow(); if (_hwnd == IntPtr.Zero) { Console.WriteLine("CreateWindowEx failed."); return 1; }
_menu = CreatePersistentMenu(); if (_menu == IntPtr.Zero) { Console.WriteLine("CreatePopupMenu failed."); return 1; }
if (!CreateTrayIcon(_hwnd)) { Console.WriteLine("Shell_NotifyIcon(NIM_ADD) failed."); return 1; }
Console.WriteLine("Tray icon created."); Console.WriteLine("Right click tray icon and see whether popup menu follows Windows dark mode.");
MSG msg; while (_running && GetMessage(out msg, IntPtr.Zero, 0, 0) > 0) { TranslateMessage(ref msg); DispatchMessage(ref msg); }
Cleanup(); return 0; }
private static void TryEnableAppDarkMode() { if (!OperatingSystem.IsWindows()) { return; }
Version v = Environment.OSVersion.Version; bool supported = v.Major == 10 && v.Build >= 17763; if (!supported) { Console.WriteLine($"Windows build {v.Build} < 17763, skipping dark mode init."); return; }
IntPtr hUxTheme = LoadLibrary("uxtheme.dll"); if (hUxTheme == IntPtr.Zero) { Console.WriteLine("LoadLibrary(uxtheme.dll) failed."); return; }
if (v.Build < 18362) { IntPtr pAllowDarkModeForApp = GetProcAddressByOrdinal(hUxTheme, 135); if (pAllowDarkModeForApp != IntPtr.Zero) { var fn = Marshal.GetDelegateForFunctionPointer<AllowDarkModeForAppDelegate>(pAllowDarkModeForApp); bool ok = fn(true); Console.WriteLine($"AllowDarkModeForApp(true) => {ok}"); } else { Console.WriteLine("AllowDarkModeForApp ordinal 135 not found."); } } else { IntPtr pSetPreferredAppMode = GetProcAddressByOrdinal(hUxTheme, 135); if (pSetPreferredAppMode != IntPtr.Zero) { var fn = Marshal.GetDelegateForFunctionPointer<SetPreferredAppModeDelegate>(pSetPreferredAppMode); PreferredAppMode oldMode = fn(PreferredAppMode.AllowDark); Console.WriteLine($"SetPreferredAppMode(AllowDark) called. Previous mode: {oldMode}"); } else { Console.WriteLine("SetPreferredAppMode ordinal 135 not found."); } }
IntPtr pRefreshImmersiveColorPolicyState = GetProcAddressByOrdinal(hUxTheme, 104); if (pRefreshImmersiveColorPolicyState != IntPtr.Zero) { var fn = Marshal.GetDelegateForFunctionPointer<RefreshImmersiveColorPolicyStateDelegate>(pRefreshImmersiveColorPolicyState); fn(); Console.WriteLine("RefreshImmersiveColorPolicyState() called."); } else { Console.WriteLine("RefreshImmersiveColorPolicyState ordinal 104 not found."); } }
private static ushort RegisterWindowClass() { var wc = new WNDCLASSEX { cbSize = (uint)Marshal.SizeOf<WNDCLASSEX>(), lpfnWndProc = Marshal.GetFunctionPointerForDelegate(_wndProcDelegate!), hInstance = GetModuleHandle(null), lpszClassName = WindowClassName };
return RegisterClassEx(ref wc); }
private static IntPtr CreateNativeWindow() { IntPtr hInstance = GetModuleHandle(null);
const int WS_EX_LAYERED = 0x00080000; const int WS_EX_NOACTIVATE = 0x08000000; const int WS_EX_TOOLWINDOW = 0x00000080; const int WS_EX_TRANSPARENT = 0x00000020; const int WS_OVERLAPPED = 0x00000000;
return CreateWindowEx( WS_EX_NOACTIVATE | WS_EX_TRANSPARENT | WS_EX_LAYERED | WS_EX_TOOLWINDOW, WindowClassName, "Tray Native Experiment", WS_OVERLAPPED, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, hInstance, IntPtr.Zero); }
private static IntPtr CreatePersistentMenu() { IntPtr menu = CreatePopupMenu(); if (menu == IntPtr.Zero) { return IntPtr.Zero; }
AppendMenu(menu, MF_STRING, (UIntPtr)MenuCmdShow, "Show main window"); AppendMenu(menu, MF_SEPARATOR, UIntPtr.Zero, null); AppendMenu(menu, MF_STRING, (UIntPtr)MenuCmdExit, "Exit");
return menu; }
private static bool CreateTrayIcon(IntPtr hwnd) { IntPtr icon = LoadIcon(IntPtr.Zero, (IntPtr)32512);
var nid = new NOTIFYICONDATA { cbSize = (uint)Marshal.SizeOf<NOTIFYICONDATA>(), hWnd = hwnd, uID = 1, uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP, uCallbackMessage = TrayCallbackMessage, hIcon = icon, szTip = Tooltip };
bool ok = Shell_NotifyIcon(NIM_ADD, ref nid); if (!ok) { return false; }
nid.uVersion = NOTIFYICON_VERSION_4; Shell_NotifyIcon(NIM_SETVERSION, ref nid);
return true; }
private static void RemoveTrayIcon() { var nid = new NOTIFYICONDATA { cbSize = (uint)Marshal.SizeOf<NOTIFYICONDATA>(), hWnd = _hwnd, uID = 1 };
Shell_NotifyIcon(NIM_DELETE, ref nid); }
private static IntPtr WndProc(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam) { if (msg == TrayCallbackMessage) { int code = LOWORD(lParam);
if (code == WM_RBUTTONUP) { ShowTrayMenu(hwnd); return IntPtr.Zero; }
if (code == WM_LBUTTONUP) { Console.WriteLine("[Tray] Left click"); return IntPtr.Zero; } }
if (msg == WM_COMMAND) { int command = LOWORD(wParam);
switch (command) { case MenuCmdShow: Console.WriteLine("[Menu] Show main window"); return IntPtr.Zero;
case MenuCmdExit: Console.WriteLine("[Menu] Exit"); _running = false; PostQuitMessage(0); return IntPtr.Zero; } }
if (msg == WM_DESTROY) { _running = false; PostQuitMessage(0); return IntPtr.Zero; }
return DefWindowProc(hwnd, msg, wParam, lParam); }
private static void ShowTrayMenu(IntPtr hwnd) { if (!GetCursorPos(out POINT pt)) { return; }
SetForegroundWindow(hwnd);
uint result = TrackPopupMenu( _menu, TPM_LEFTALIGN | TPM_BOTTOMALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD, pt.X, pt.Y, 0, hwnd, IntPtr.Zero);
PostMessage(hwnd, WM_NULL, IntPtr.Zero, IntPtr.Zero);
if (result != 0) { SendMessage(hwnd, WM_COMMAND, (IntPtr)result, IntPtr.Zero); } }
private static void Cleanup() { RemoveTrayIcon();
if (_menu != IntPtr.Zero) { DestroyMenu(_menu); _menu = IntPtr.Zero; }
if (_hwnd != IntPtr.Zero) { DestroyWindow(_hwnd); _hwnd = IntPtr.Zero; } }
private static int LOWORD(IntPtr value) { return unchecked((ushort)((nuint)value & 0xFFFF)); }
private static IntPtr GetProcAddressByOrdinal(IntPtr hModule, short ordinal) { return GetProcAddress(hModule, (IntPtr)ordinal); }
private delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
[UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate bool AllowDarkModeForAppDelegate(bool allow);
[UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate PreferredAppMode SetPreferredAppModeDelegate(PreferredAppMode appMode);
[UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate void RefreshImmersiveColorPolicyStateDelegate();
private enum PreferredAppMode { Default = 0, AllowDark = 1 }
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct WNDCLASSEX { public uint cbSize; public uint style; public IntPtr lpfnWndProc; public int cbClsExtra; public int cbWndExtra; public IntPtr hInstance; public IntPtr hIcon; public IntPtr hCursor; public IntPtr hbrBackground; [MarshalAs(UnmanagedType.LPWStr)] public string? lpszMenuName; [MarshalAs(UnmanagedType.LPWStr)] public string lpszClassName; public IntPtr hIconSm; }
[StructLayout(LayoutKind.Sequential)] private struct POINT { public int X; public int Y; }
[StructLayout(LayoutKind.Sequential)] private struct MSG { public IntPtr hwnd; public uint message; public IntPtr wParam; public IntPtr lParam; public uint time; public POINT pt; public uint lPrivate; }
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] private struct NOTIFYICONDATA { public uint cbSize; public IntPtr hWnd; public uint uID; public uint uFlags; public uint uCallbackMessage; public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] public string szTip;
public uint dwState; public uint dwStateMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string szInfo;
public uint uTimeoutOrVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)] public string szInfoTitle;
public uint dwInfoFlags; public Guid guidItem; public IntPtr hBalloonIcon;
public uint uVersion { get => uTimeoutOrVersion; set => uTimeoutOrVersion = value; } }
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern ushort RegisterClassEx(ref WNDCLASSEX lpwcx);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern IntPtr CreateWindowEx( int dwExStyle, string lpClassName, string lpWindowName, int dwStyle, int X, int Y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
[DllImport("user32.dll", SetLastError = true)] private static extern bool DestroyWindow(IntPtr hWnd);
[DllImport("user32.dll")] private static extern IntPtr DefWindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")] private static extern sbyte GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
[DllImport("user32.dll")] private static extern bool TranslateMessage(ref MSG lpMsg);
[DllImport("user32.dll")] private static extern IntPtr DispatchMessage(ref MSG lpmsg);
[DllImport("user32.dll")] private static extern void PostQuitMessage(int nExitCode);
[DllImport("user32.dll", SetLastError = true)] private static extern bool PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)] private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)] private static extern IntPtr CreatePopupMenu();
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern bool AppendMenu(IntPtr hMenu, uint uFlags, UIntPtr uIDNewItem, string? lpNewItem);
[DllImport("user32.dll", SetLastError = true)] private static extern bool DestroyMenu(IntPtr hMenu);
[DllImport("user32.dll", SetLastError = true)] private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)] private static extern uint TrackPopupMenu( IntPtr hMenu, uint uFlags, int x, int y, int nReserved, IntPtr hWnd, IntPtr prcRect);
[DllImport("user32.dll", SetLastError = true)] private static extern bool GetCursorPos(out POINT lpPoint);
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern bool Shell_NotifyIcon(int dwMessage, ref NOTIFYICONDATA lpData);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern IntPtr GetModuleHandle(string? lpModuleName);
[DllImport("user32.dll", SetLastError = true)] private static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr lpIconName);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress", SetLastError = true)] private static extern IntPtr GetProcAddress(IntPtr hModule, IntPtr procName); }
|