@@ -122,7 +122,8 @@ protected override void OnAttached()
122122 this . AssociatedObject . Deactivated += ( sender , args ) => { topmostHack ( ) ; } ;
123123
124124 this . AssociatedObject . Loaded += this . AssociatedObject_Loaded ;
125- this . AssociatedObject . Unloaded += this . AssociatedObject_Unloaded ;
125+ this . AssociatedObject . Unloaded += this . AssociatedObjectUnloaded ;
126+ this . AssociatedObject . Closed += this . AssociatedObjectClosed ;
126127 this . AssociatedObject . SourceInitialized += this . AssociatedObject_SourceInitialized ;
127128 this . AssociatedObject . StateChanged += this . OnAssociatedObjectHandleMaximize ;
128129
@@ -229,6 +230,11 @@ private void Cleanup()
229230 {
230231 this . isCleanedUp = true ;
231232
233+ if ( GetHandleTaskbar ( this . AssociatedObject ) && this . isWindwos10OrHigher )
234+ {
235+ this . DeactivateTaskbarFix ( ) ;
236+ }
237+
232238 // clean up events
233239 if ( this . AssociatedObject is MetroWindow )
234240 {
@@ -238,7 +244,8 @@ private void Cleanup()
238244 . RemoveValueChanged ( this . AssociatedObject , this . UseNoneWindowStylePropertyChangedCallback ) ;
239245 }
240246 this . AssociatedObject . Loaded -= this . AssociatedObject_Loaded ;
241- this . AssociatedObject . Unloaded -= this . AssociatedObject_Unloaded ;
247+ this . AssociatedObject . Unloaded -= this . AssociatedObjectUnloaded ;
248+ this . AssociatedObject . Closed -= this . AssociatedObjectClosed ;
242249 this . AssociatedObject . SourceInitialized -= this . AssociatedObject_SourceInitialized ;
243250 this . AssociatedObject . StateChanged -= this . OnAssociatedObjectHandleMaximize ;
244251 if ( this . hwndSource != null )
@@ -255,7 +262,12 @@ protected override void OnDetaching()
255262 base . OnDetaching ( ) ;
256263 }
257264
258- private void AssociatedObject_Unloaded ( object sender , RoutedEventArgs e )
265+ private void AssociatedObjectUnloaded ( object sender , RoutedEventArgs e )
266+ {
267+ this . Cleanup ( ) ;
268+ }
269+
270+ private void AssociatedObjectClosed ( object sender , EventArgs e )
259271 {
260272 this . Cleanup ( ) ;
261273 }
@@ -315,34 +327,17 @@ private void HandleMaximize()
315327 var monitorInfo = new MONITORINFO ( ) ;
316328 UnsafeNativeMethods . GetMonitorInfo ( monitor , monitorInfo ) ;
317329
318- if ( ignoreTaskBar )
330+ var x = monitorInfo . rcMonitor . left ;
331+ var y = monitorInfo . rcMonitor . top ;
332+ var cx = Math . Abs ( monitorInfo . rcMonitor . right - x ) ;
333+ var cy = Math . Abs ( monitorInfo . rcMonitor . bottom - y ) ;
334+
335+ if ( ignoreTaskBar && this . isWindwos10OrHigher )
319336 {
320- var x = monitorInfo . rcMonitor . left ;
321- var y = monitorInfo . rcMonitor . top ;
322- var cx = Math . Abs ( monitorInfo . rcMonitor . right - x ) ;
323- var cy = Math . Abs ( monitorInfo . rcMonitor . bottom - y ) ;
324-
325- var trayHWND = Standard . NativeMethods . FindWindow ( "Shell_TrayWnd" , null ) ;
326- if ( this . isWindwos10OrHigher && trayHWND != IntPtr . Zero )
327- {
328- UnsafeNativeMethods . SetWindowPos ( this . handle , trayHWND , x , y , cx , cy , 0x0040 ) ;
329- Standard . NativeMethods . ShowWindow ( this . handle , Standard . SW . HIDE ) ;
330- Standard . NativeMethods . ShowWindow ( this . handle , Standard . SW . SHOW ) ;
331- }
332- else
333- {
334- UnsafeNativeMethods . SetWindowPos ( this . handle , new IntPtr ( - 2 ) , x , y , cx , cy , 0x0040 ) ;
335- }
337+ this . ActivateTaskbarFix ( ) ;
336338 }
337- else
338- {
339- var x = monitorInfo . rcWork . left ;
340- var y = monitorInfo . rcWork . top ;
341- var cx = Math . Abs ( monitorInfo . rcWork . right - x ) ;
342- var cy = Math . Abs ( monitorInfo . rcWork . bottom - y ) ;
343339
344- UnsafeNativeMethods . SetWindowPos ( this . handle , new IntPtr ( - 2 ) , x , y , cx , cy , 0x0040 ) ;
345- }
340+ UnsafeNativeMethods . SetWindowPos ( this . handle , new IntPtr ( - 2 ) , x , y , cx , cy , 0x0040 ) ;
346341 }
347342 }
348343 }
@@ -360,23 +355,9 @@ private void HandleMaximize()
360355
361356 // #2694 make sure the window is not on top after restoring window
362357 // this issue was introduced after fixing the windows 10 bug with the taskbar and a maximized window that ignores the taskbar
363- RECT rect ;
364- if ( UnsafeNativeMethods . GetWindowRect ( this . handle , out rect ) )
358+ if ( GetHandleTaskbar ( this . AssociatedObject ) && this . isWindwos10OrHigher )
365359 {
366- var left = rect . left ;
367- var top = rect . top ;
368- var width = rect . Width ;
369- var height = rect . Height ;
370-
371- // #2780 Don't blindly set the Z-Order to HWWND_NOTOPMOST. If this window has an owner, set
372- // the Z-Order to be after the owner. This keeps external dialogs appearing correctly above
373- // their owner window even when owner window is maximized and ignoring taskbar.
374- IntPtr hwndInsAfter = Constants . HWND_NOTOPMOST ;
375- if ( this . AssociatedObject . Owner != null )
376- {
377- hwndInsAfter = new WindowInteropHelper ( this . AssociatedObject . Owner ) . Handle ;
378- }
379- UnsafeNativeMethods . SetWindowPos ( this . handle , hwndInsAfter , left , top , width , height , 0x0040 ) ;
360+ this . DeactivateTaskbarFix ( ) ;
380361 }
381362 }
382363
@@ -401,6 +382,46 @@ private void HandleMaximize()
401382 this . topMostChangeNotifier . RaiseValueChanged = raiseValueChanged ;
402383 }
403384
385+ private void ActivateTaskbarFix ( )
386+ {
387+ var trayWndHandle = Standard . NativeMethods . FindWindow ( "Shell_TrayWnd" , null ) ;
388+ if ( trayWndHandle != IntPtr . Zero )
389+ {
390+ SetHandleTaskbar ( this . AssociatedObject , true ) ;
391+ UnsafeNativeMethods . SetWindowPos ( trayWndHandle , Constants . HWND_BOTTOM , 0 , 0 , 0 , 0 , Constants . TOPMOST_FLAGS ) ;
392+ UnsafeNativeMethods . SetWindowPos ( trayWndHandle , Constants . HWND_TOP , 0 , 0 , 0 , 0 , Constants . TOPMOST_FLAGS ) ;
393+ UnsafeNativeMethods . SetWindowPos ( trayWndHandle , Constants . HWND_NOTOPMOST , 0 , 0 , 0 , 0 , Constants . TOPMOST_FLAGS ) ;
394+ }
395+ }
396+
397+ private void DeactivateTaskbarFix ( )
398+ {
399+ var trayWndHandle = Standard . NativeMethods . FindWindow ( "Shell_TrayWnd" , null ) ;
400+ if ( trayWndHandle != IntPtr . Zero )
401+ {
402+ SetHandleTaskbar ( this . AssociatedObject , false ) ;
403+ UnsafeNativeMethods . SetWindowPos ( trayWndHandle , Constants . HWND_BOTTOM , 0 , 0 , 0 , 0 , Constants . TOPMOST_FLAGS ) ;
404+ UnsafeNativeMethods . SetWindowPos ( trayWndHandle , Constants . HWND_TOP , 0 , 0 , 0 , 0 , Constants . TOPMOST_FLAGS ) ;
405+ UnsafeNativeMethods . SetWindowPos ( trayWndHandle , Constants . HWND_TOPMOST , 0 , 0 , 0 , 0 , Constants . TOPMOST_FLAGS ) ;
406+ }
407+ }
408+
409+ private static readonly DependencyProperty HandleTaskbarProperty
410+ = DependencyProperty . RegisterAttached (
411+ "HandleTaskbar" ,
412+ typeof ( bool ) ,
413+ typeof ( BorderlessWindowBehavior ) , new FrameworkPropertyMetadata ( false ) ) ;
414+
415+ private static bool GetHandleTaskbar ( UIElement element )
416+ {
417+ return ( bool ) element . GetValue ( HandleTaskbarProperty ) ;
418+ }
419+
420+ private static void SetHandleTaskbar ( UIElement element , bool value )
421+ {
422+ element . SetValue ( HandleTaskbarProperty , value ) ;
423+ }
424+
404425 private void AssociatedObject_SourceInitialized ( object sender , EventArgs e )
405426 {
406427 this . handle = new WindowInteropHelper ( this . AssociatedObject ) . Handle ;
0 commit comments