1+ using System ;
2+ using System . Runtime . InteropServices ;
3+
4+ internal static partial class DwmInterop
5+ {
6+ internal static Boolean Initialized { get => _initialized ; }
7+ private static Boolean _initialized = false ;
8+
9+ internal static Boolean Initialize ( )
10+ {
11+ if ( _initialized ) return false ;
12+
13+ Object regOutput = Microsoft . Win32 . Registry . GetValue ( "HKEY_LOCAL_MACHINE\\ SOFTWARE\\ Microsoft\\ Windows NT\\ CurrentVersion" , "CurrentBuildNumber" , null ) ! ;
14+
15+ if ( regOutput == null ) return false ;
16+ if ( regOutput is not String ) return false ;
17+ if ( ! Int32 . TryParse ( ( String ) regOutput , out _windowsBuildNumber ) ) return false ;
18+ if ( _windowsBuildNumber < 0 ) return false ;
19+
20+ if ( _windowsBuildNumber >= 18985 ) // windows 10 '20H1' or newer
21+ {
22+ _dwmDarkModeWindowAttribute = DWMWINDOWATTRIBUTE . DWMWA_USE_IMMERSIVE_DARK_MODE ;
23+ _darkModeCompatibilityLevel = DWM_Dark_Mode_Compatibility_Level . IMMERSIVE_DARK_MODE ;
24+ }
25+ else if ( _windowsBuildNumber >= 17763 )
26+ {
27+ _dwmDarkModeWindowAttribute = DWMWINDOWATTRIBUTE . DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_18985_EQUAL_OR_AFTER_17763 ;
28+ _darkModeCompatibilityLevel = DWM_Dark_Mode_Compatibility_Level . IMMERSIVE_DARK_MODE_BEFORE_18985_EQUAL_OR_AFTER_17763 ;
29+ }
30+ else
31+ {
32+ _dwmDarkModeWindowAttribute = 0 ;
33+ _darkModeCompatibilityLevel = DWM_Dark_Mode_Compatibility_Level . NONE ;
34+ }
35+
36+ _initialized = true ;
37+ return true ;
38+ }
39+
40+ // # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
41+
42+ [ LibraryImport ( "dwmapi.dll" , SetLastError = true ) ]
43+ [ return : MarshalAs ( UnmanagedType . U4 ) ]
44+ private static unsafe partial UInt32 DwmSetWindowAttribute ( IntPtr hwnd , DWMWINDOWATTRIBUTE dwAttribute , UInt32 * pvAttribute , UInt32 cbAttribute ) ;
45+
46+ // # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
47+
48+ /// <summary>
49+ /// Requires OS version 17763 or later
50+ /// </summary>
51+ /// <param name="hwnd"></param>
52+ /// <param name="darkMode"></param>
53+ /// <returns>HRESULT</returns>
54+ /// <exception cref="InvalidOperationException"></exception>
55+ internal static unsafe UInt32 SetTheme ( IntPtr hwnd , Boolean darkMode )
56+ {
57+ if ( ! _initialized ) throw new Exception ( "DwmInterop not initialized." ) ;
58+
59+ UInt32 expandedBoolean = * ( UInt32 * ) & darkMode ;
60+
61+ return DwmSetWindowAttribute ( hwnd , _dwmDarkModeWindowAttribute , & expandedBoolean , sizeof ( UInt32 ) ) ;
62+ }
63+
64+ /// <summary>
65+ /// Requires OS version 22000 or later
66+ /// </summary>
67+ /// <param name="hwnd"></param>
68+ /// <param name="COLORREF"></param>
69+ /// <returns>HRESULT</returns>
70+ /// <exception cref="Exception"></exception>
71+ internal static unsafe UInt32 SetCaptionColor ( IntPtr hwnd , UInt32 COLORREF )
72+ {
73+ if ( ! _initialized ) throw new Exception ( "DwmInterop not initialized." ) ;
74+
75+ return DwmSetWindowAttribute ( hwnd , DWMWINDOWATTRIBUTE . DWMWA_CAPTION_COLOR , & COLORREF , sizeof ( UInt32 ) ) ;
76+ }
77+
78+ /// <summary>
79+ /// Requires OS version 22000 or later
80+ /// </summary>
81+ /// <param name="hwnd"></param>
82+ /// <param name="COLORREF"></param>
83+ /// <returns>HRESULT</returns>
84+ /// <exception cref="Exception"></exception>
85+ internal static unsafe UInt32 SetBorderColor ( IntPtr hwnd , UInt32 COLORREF )
86+ {
87+ if ( ! _initialized ) throw new Exception ( "DwmInterop not initialized." ) ;
88+
89+ return DwmSetWindowAttribute ( hwnd , DWMWINDOWATTRIBUTE . DWMWA_BORDER_COLOR , & COLORREF , sizeof ( UInt32 ) ) ;
90+ }
91+
92+ // # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
93+
94+ internal static DWM_Dark_Mode_Compatibility_Level DarkModeCompatibilityLevel { get => _darkModeCompatibilityLevel ; }
95+ private static DWM_Dark_Mode_Compatibility_Level _darkModeCompatibilityLevel ;
96+
97+ internal enum DWM_Dark_Mode_Compatibility_Level : Int32
98+ {
99+ NONE = 0 ,
100+ IMMERSIVE_DARK_MODE_BEFORE_18985_EQUAL_OR_AFTER_17763 = 1 ,
101+ IMMERSIVE_DARK_MODE = 2 ,
102+ }
103+
104+ private static DWMWINDOWATTRIBUTE _dwmDarkModeWindowAttribute = 0 ;
105+ private enum DWMWINDOWATTRIBUTE : UInt32
106+ {
107+ DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_18985_EQUAL_OR_AFTER_17763 = 19 ,
108+ DWMWA_USE_IMMERSIVE_DARK_MODE = 20 ,
109+ DWMWA_WINDOW_CORNER_PREFERENCE = 33 ,
110+ DWMWA_BORDER_COLOR = 34 ,
111+ DWMWA_CAPTION_COLOR = 35 ,
112+ DWMWA_TEXT_COLOR = 36
113+ }
114+
115+ private static Int32 _windowsBuildNumber = - 1 ;
116+ }
0 commit comments