10
10
using MahApps . Metro . Native ;
11
11
using System . Windows . Shapes ;
12
12
using System . Collections . Generic ;
13
+ using System . Reflection ;
13
14
using System . Windows . Controls . Primitives ;
15
+ using System . Windows . Threading ;
14
16
15
17
namespace MahApps . Metro . Controls
16
18
{
@@ -899,11 +901,23 @@ public override void OnApplyTemplate()
899
901
this . SetVisibiltyForAllTitleElements ( this . TitlebarHeight > 0 ) ;
900
902
}
901
903
904
+ protected IntPtr CriticalHandle
905
+ {
906
+ get
907
+ {
908
+ var value = typeof ( Window )
909
+ . GetProperty ( "CriticalHandle" , BindingFlags . NonPublic | BindingFlags . Instance )
910
+ . GetValue ( this , new object [ 0 ] ) ;
911
+ return ( IntPtr ) value ;
912
+ }
913
+ }
914
+
902
915
private void ClearWindowEvents ( )
903
916
{
904
917
// clear all event handlers first:
905
918
if ( this . windowTitleThumb != null )
906
919
{
920
+ this . windowTitleThumb . PreviewMouseLeftButtonUp -= WindowTitleThumbOnPreviewMouseLeftButtonUp ;
907
921
this . windowTitleThumb . DragDelta -= this . WindowTitleThumbMoveOnDragDelta ;
908
922
this . windowTitleThumb . MouseDoubleClick -= this . WindowTitleThumbChangeWindowStateOnMouseDoubleClick ;
909
923
this . windowTitleThumb . MouseRightButtonUp -= this . WindowTitleThumbSystemMenuOnMouseRightButtonUp ;
@@ -928,6 +942,7 @@ private void SetWindowEvents()
928
942
929
943
if ( this . windowTitleThumb != null )
930
944
{
945
+ this . windowTitleThumb . PreviewMouseLeftButtonUp += WindowTitleThumbOnPreviewMouseLeftButtonUp ;
931
946
this . windowTitleThumb . DragDelta += this . WindowTitleThumbMoveOnDragDelta ;
932
947
this . windowTitleThumb . MouseDoubleClick += this . WindowTitleThumbChangeWindowStateOnMouseDoubleClick ;
933
948
this . windowTitleThumb . MouseRightButtonUp += this . WindowTitleThumbSystemMenuOnMouseRightButtonUp ;
@@ -955,6 +970,11 @@ private void IconMouseDown(object sender, MouseButtonEventArgs e)
955
970
}
956
971
}
957
972
973
+ private void WindowTitleThumbOnPreviewMouseLeftButtonUp ( object sender , MouseButtonEventArgs e )
974
+ {
975
+ DoWindowTitleThumbOnPreviewMouseLeftButtonUp ( this , e ) ;
976
+ }
977
+
958
978
private void WindowTitleThumbMoveOnDragDelta ( object sender , DragDeltaEventArgs dragDeltaEventArgs )
959
979
{
960
980
DoWindowTitleThumbMoveOnDragDelta ( this , dragDeltaEventArgs ) ;
@@ -970,18 +990,24 @@ private void WindowTitleThumbSystemMenuOnMouseRightButtonUp(object sender, Mouse
970
990
DoWindowTitleThumbSystemMenuOnMouseRightButtonUp ( this , e ) ;
971
991
}
972
992
993
+ internal static void DoWindowTitleThumbOnPreviewMouseLeftButtonUp ( MetroWindow window , MouseButtonEventArgs mouseButtonEventArgs )
994
+ {
995
+ Mouse . Capture ( null ) ;
996
+ }
997
+
973
998
internal static void DoWindowTitleThumbMoveOnDragDelta ( MetroWindow window , DragDeltaEventArgs dragDeltaEventArgs )
974
999
{
975
1000
// drag only if IsWindowDraggable is set to true
976
1001
if ( ! window . IsWindowDraggable ||
977
- ( ! ( Math . Abs ( dragDeltaEventArgs . HorizontalChange ) > 2 ) &&
978
- ! ( Math . Abs ( dragDeltaEventArgs . VerticalChange ) > 2 ) ) ) return ;
1002
+ ( ! ( Math . Abs ( dragDeltaEventArgs . HorizontalChange ) > 2 ) && ! ( Math . Abs ( dragDeltaEventArgs . VerticalChange ) > 2 ) ) )
1003
+ {
1004
+ return ;
1005
+ }
979
1006
980
- var windowHandle = new WindowInteropHelper ( window ) . Handle ;
981
- var cursorPos = Standard . NativeMethods . GetCursorPos ( ) ;
1007
+ // tage from DragMove internal code
1008
+ window . VerifyAccess ( ) ;
982
1009
983
- // for the touch usage
984
- UnsafeNativeMethods . ReleaseCapture ( ) ;
1010
+ var cursorPos = Standard . NativeMethods . GetCursorPos ( ) ;
985
1011
986
1012
// if the window is maximized dragging is only allowed on title bar (also if not visible)
987
1013
var windowIsMaximized = window . WindowState == WindowState . Maximized ;
@@ -991,14 +1017,30 @@ internal static void DoWindowTitleThumbMoveOnDragDelta(MetroWindow window, DragD
991
1017
return ;
992
1018
}
993
1019
994
- if ( windowIsMaximized )
995
- {
1020
+ // for the touch usage
1021
+ UnsafeNativeMethods . ReleaseCapture ( ) ;
1022
+
1023
+ if ( windowIsMaximized ) {
996
1024
window . Top = 2 ;
997
1025
window . Left = Math . Max ( cursorPos . x - window . RestoreBounds . Width / 2 , 0 ) ;
1026
+ EventHandler windowOnStateChanged = null ;
1027
+ windowOnStateChanged = ( sender , args ) =>
1028
+ {
1029
+ window . StateChanged -= windowOnStateChanged ;
1030
+ if ( window . WindowState == WindowState . Normal )
1031
+ {
1032
+ Mouse . Capture ( window . windowTitleThumb , CaptureMode . Element ) ;
1033
+ }
1034
+ } ;
1035
+ window . StateChanged += windowOnStateChanged ;
998
1036
}
999
- var lParam = ( int ) ( uint ) cursorPos . x | ( cursorPos . y << 16 ) ;
1000
- Standard . NativeMethods . SendMessage ( windowHandle , Standard . WM . LBUTTONUP , ( IntPtr ) Standard . HT . CAPTION , ( IntPtr ) lParam ) ;
1001
- Standard . NativeMethods . SendMessage ( windowHandle , Standard . WM . SYSCOMMAND , ( IntPtr ) Standard . SC . MOUSEMOVE , IntPtr . Zero ) ;
1037
+
1038
+ var criticalHandle = window . CriticalHandle ;
1039
+ // DragMove works too
1040
+ // window.DragMove();
1041
+ // instead this 2 lines
1042
+ Standard . NativeMethods . SendMessage ( criticalHandle , Standard . WM . SYSCOMMAND , ( IntPtr ) Standard . SC . MOUSEMOVE , IntPtr . Zero ) ;
1043
+ Standard . NativeMethods . SendMessage ( criticalHandle , Standard . WM . LBUTTONUP , IntPtr . Zero , IntPtr . Zero ) ;
1002
1044
}
1003
1045
1004
1046
internal static void DoWindowTitleThumbChangeWindowStateOnMouseDoubleClick ( MetroWindow window , MouseButtonEventArgs mouseButtonEventArgs )
0 commit comments