@@ -14,8 +14,8 @@ namespace NETworkManager.Utilities.WPF;
1414/// <remarks>
1515/// WPF removes <c>WS_EX_LAYERED</c> from a top-level window without <see cref="Window.AllowsTransparency" /> in
1616/// <c>WM_STYLECHANGING</c> (<c>HwndTarget</c>: "prevents an external program from making us system-layered"), also when
17- /// it updates its render settings later. A hook of the window (called before the one of WPF) keeps the style while the
18- /// window should be transparent.
17+ /// it updates its render settings later. A hook of the window (called before the one of WPF) keeps the style; it is only
18+ /// added while the window is transparent. Disabled (0) on a window that was never transparent changes nothing .
1919/// </remarks>
2020public static class WindowTransparencyHelper
2121{
@@ -50,14 +50,7 @@ public static class WindowTransparencyHelper
5050 private static extern bool RedrawWindow ( IntPtr hWnd , IntPtr lprcUpdate , IntPtr hrgnUpdate , uint flags ) ;
5151
5252 /// <summary>
53- /// Transparency of the window in percent that the hook keeps (0 = WPF may remove the layered style again).
54- /// </summary>
55- private static readonly DependencyProperty TransparencyProperty =
56- DependencyProperty . RegisterAttached ( "Transparency" , typeof ( int ) , typeof ( WindowTransparencyHelper ) ,
57- new PropertyMetadata ( 0 ) ) ;
58-
59- /// <summary>
60- /// Hook of the window, added once.
53+ /// Hook of the window, only set while the window is transparent.
6154 /// </summary>
6255 private static readonly DependencyProperty HookProperty =
6356 DependencyProperty . RegisterAttached ( "Hook" , typeof ( HwndSourceHook ) , typeof ( WindowTransparencyHelper ) ,
@@ -77,44 +70,43 @@ public static byte GetAlpha(int transparency)
7770 }
7871
7972 /// <summary>
80- /// Makes the window transparent (<paramref name="transparency" /> in percent) or opaque again (0). Does nothing
81- /// before the window has a handle (call it in / after SourceInitialized).
73+ /// Makes the window transparent (<paramref name="transparency" /> in percent) or opaque again (0). With 0 on a window
74+ /// that was never transparent, nothing is changed (no hook, no style). Does nothing before the window has a handle
75+ /// (call it in / after SourceInitialized).
8276 /// </summary>
8377 /// <param name="window">Window to change.</param>
84- /// <param name="transparency">Transparency in percent (0 = opaque, removes the layered style again).</param>
78+ /// <param name="transparency">Transparency in percent (0 = opaque, removes the hook and the layered style again).</param>
8579 public static void Apply ( Window window , int transparency )
8680 {
87- var handle = new WindowInteropHelper ( window ) . Handle ;
88-
89- if ( handle == IntPtr . Zero )
90- return ;
91-
9281 transparency = Math . Clamp ( transparency , 0 , 100 ) ;
9382
94- // The hook keeps the layered style as long as this value is > 0 , so set it before the style is changed
95- window . SetValue ( TransparencyProperty , transparency ) ;
83+ // The hook is only added while the window is transparent , so it also marks a window changed here
84+ var hook = ( HwndSourceHook ) window . GetValue ( HookProperty ) ;
9685
97- if ( transparency > 0 && window . GetValue ( HookProperty ) == null )
98- {
99- var source = HwndSource . FromHwnd ( handle ) ;
86+ // Disabled and never enabled: don't touch the window (same behavior as without this option )
87+ if ( transparency == 0 && hook == null )
88+ return ;
10089
101- if ( source == null )
102- return ;
90+ var handle = new WindowInteropHelper ( window ) . Handle ;
10391
104- HwndSourceHook hook = ( IntPtr _ , int msg , IntPtr wParam , IntPtr lParam , ref bool handled ) =>
105- WndProc ( window , msg , wParam , lParam , ref handled ) ;
92+ if ( handle == IntPtr . Zero )
93+ return ;
10694
107- source . AddHook ( hook ) ;
108- window . SetValue ( HookProperty , hook ) ;
109- }
95+ var source = HwndSource . FromHwnd ( handle ) ;
96+
97+ if ( source == null )
98+ return ;
11099
111100 var exStyle = GetWindowLongPtr ( handle , GWL_EXSTYLE ) . ToInt64 ( ) ;
112101 var isLayered = ( exStyle & WS_EX_LAYERED ) != 0 ;
113102
114103 if ( transparency == 0 )
115104 {
116- // Opaque: remove the layered style again, so a disabled option has no side effects (WPF only allows it
117- // with the hook, so it was set here)
105+ // Opaque again: remove the hook first, then the layered style (WPF only allows it with the hook, so it was
106+ // set here), so the window is the same as before
107+ source . RemoveHook ( hook ) ;
108+ window . ClearValue ( HookProperty ) ;
109+
118110 if ( ! isLayered )
119111 return ;
120112
@@ -126,19 +118,26 @@ public static void Apply(Window window, int transparency)
126118 return ;
127119 }
128120
121+ // Add the hook before the style is changed, so WPF doesn't remove the layered style again
122+ if ( hook == null )
123+ {
124+ hook = WndProc ;
125+ source . AddHook ( hook ) ;
126+ window . SetValue ( HookProperty , hook ) ;
127+ }
128+
129129 if ( ! isLayered )
130130 SetWindowLongPtr ( handle , GWL_EXSTYLE , new IntPtr ( exStyle | WS_EX_LAYERED ) ) ;
131131
132132 SetLayeredWindowAttributes ( handle , 0 , GetAlpha ( transparency ) , LWA_ALPHA ) ;
133133 }
134134
135- private static IntPtr WndProc ( Window window , int msg , IntPtr wParam , IntPtr lParam , ref bool handled )
135+ private static IntPtr WndProc ( IntPtr hwnd , int msg , IntPtr wParam , IntPtr lParam , ref bool handled )
136136 {
137- // Keep the layered style while the window should be transparent. Handled, so WPF (HwndTarget) doesn't remove
138- // it again. STYLESTRUCT: styleOld (offset 0), styleNew (offset 4).
139- // wParam unchecked like WPF: ToInt32() throws on 64-bit if the value doesn't fit
140- if ( msg != WM_STYLECHANGING || unchecked ( ( int ) wParam . ToInt64 ( ) ) != GWL_EXSTYLE ||
141- ( int ) window . GetValue ( TransparencyProperty ) == 0 )
137+ // Keep the layered style (the hook is only added while the window is transparent). Handled, so WPF
138+ // (HwndTarget) doesn't remove it again. STYLESTRUCT: styleOld (offset 0), styleNew (offset 4). wParam unchecked
139+ // like WPF: ToInt32() throws on 64-bit if the value doesn't fit.
140+ if ( msg != WM_STYLECHANGING || unchecked ( ( int ) wParam . ToInt64 ( ) ) != GWL_EXSTYLE )
142141 return IntPtr . Zero ;
143142
144143 var styleNew = Marshal . ReadInt32 ( lParam , 4 ) ;
0 commit comments