11using System ;
22using System . Collections . Generic ;
33using System . ComponentModel ;
4+ using System . Diagnostics ;
45using System . Linq ;
56using System . Runtime . CompilerServices ;
67using System . Threading . Tasks ;
78using System . Windows ;
89using System . Windows . Forms ;
910using System . Windows . Input ;
11+ using System . Windows . Interop ;
1012using Dragablz ;
1113using MahApps . Metro . Controls . Dialogs ;
1214using NETworkManager . Localization ;
@@ -60,21 +62,16 @@ private async void FocusEmbeddedWindow()
6062 if ( ! _embeddedWindowApplicationNames . Contains ( ApplicationName ) )
6163 return ;
6264
63- var window = Application . Current . Windows . OfType < Window > ( ) . FirstOrDefault ( x => x . IsActive ) ;
64-
65- if ( window == null )
66- return ;
67-
6865 // Find all TabablzControl in the active window
69- foreach ( var tabablzControl in VisualTreeHelper . FindVisualChildren < TabablzControl > ( window ) )
66+ foreach ( var tabablzControl in VisualTreeHelper . FindVisualChildren < TabablzControl > ( this ) )
7067 {
7168 // Skip if no items
7269 if ( tabablzControl . Items . Count == 0 )
7370 continue ;
7471
7572 // Focus embedded window in the selected tab
7673 ( ( ( DragablzTabItem ) tabablzControl . SelectedItem ) ? . View as IEmbeddedWindow ) ? . FocusEmbeddedWindow ( ) ;
77-
74+
7875 break ;
7976 }
8077 }
@@ -212,7 +209,7 @@ private void RemoteDesktop_FullscreenAction(object view)
212209 private void RemoteDesktop_AdjustScreenAction ( object view )
213210 {
214211 if ( view is RemoteDesktopControl control )
215- control . AdjustScreen ( force : true ) ;
212+ control . AdjustScreen ( force : true ) ;
216213 }
217214
218215 public ICommand RemoteDesktop_SendCtrlAltDelCommand =>
@@ -377,14 +374,8 @@ private void MetroWindow_Activated(object sender, EventArgs e)
377374
378375 private void DragablzTabHostWindow_OnClosing ( object sender , CancelEventArgs e )
379376 {
380- // Close all tabs properly when the window is closing
381- var window = Application . Current . Windows . OfType < Window > ( ) . FirstOrDefault ( x => x . IsActive ) ;
382-
383- if ( window == null )
384- return ;
385-
386377 // Find all TabablzControl in the active window
387- foreach ( var tabablzControl in VisualTreeHelper . FindVisualChildren < TabablzControl > ( window ) )
378+ foreach ( var tabablzControl in VisualTreeHelper . FindVisualChildren < TabablzControl > ( this ) )
388379 foreach ( var tabItem in tabablzControl . Items . OfType < DragablzTabItem > ( ) )
389380 ( ( IDragablzTabItem ) tabItem . View ) . CloseTab ( ) ;
390381
@@ -439,4 +430,72 @@ private void TabablzControl_OnIsDraggingWindowChanged(object sender, RoutedPrope
439430 }
440431
441432 #endregion
433+
434+ #region Handle WndProc messages (handle window size events)
435+
436+ private HwndSource _hwndSource ;
437+
438+ private const int WmExitSizeMove = 0x232 ;
439+ private const int WmSysCommand = 0x0112 ;
440+ private const int ScMaximize = 0xF030 ;
441+ private const int ScRestore = 0xF120 ;
442+
443+ protected override void OnSourceInitialized ( EventArgs e )
444+ {
445+ base . OnSourceInitialized ( e ) ;
446+
447+ _hwndSource = HwndSource . FromHwnd ( new WindowInteropHelper ( this ) . Handle ) ;
448+ _hwndSource ? . AddHook ( HwndHook ) ;
449+ }
450+
451+ [ DebuggerStepThrough ]
452+ private IntPtr HwndHook ( IntPtr hwnd , int msg , IntPtr wParam , IntPtr lParam , ref bool handled )
453+ {
454+ // Window size events
455+ switch ( msg )
456+ {
457+ // Handle window resize and move events
458+ case WmExitSizeMove :
459+ UpdateOnWindowResize ( ) ;
460+ break ;
461+
462+ // Handle system commands (like maximize and restore)
463+ case WmSysCommand :
464+
465+ switch ( wParam . ToInt32 ( ) )
466+ {
467+ // Window is maximized
468+ case ScMaximize :
469+ // Window is restored (back to normal size from maximized state)
470+ case ScRestore :
471+ UpdateOnWindowResize ( ) ;
472+ break ;
473+ }
474+
475+ break ;
476+ }
477+
478+ handled = false ;
479+
480+ return IntPtr . Zero ;
481+ }
482+
483+ private void UpdateOnWindowResize ( )
484+ {
485+ // Find all TabablzControl
486+ foreach ( var tabablzControl in VisualTreeHelper . FindVisualChildren < TabablzControl > ( this ) )
487+ {
488+ // Skip if no items
489+ if ( tabablzControl . Items . Count == 0 )
490+ continue ;
491+
492+ foreach ( var item in tabablzControl . Items . OfType < DragablzTabItem > ( ) )
493+ {
494+ if ( item . View is RemoteDesktopControl control )
495+ control . UpdateOnWindowResize ( ) ;
496+ }
497+ }
498+ }
499+
500+ #endregion
442501}
0 commit comments