diff --git a/MahApps.Metro/Controls/MetroWindow.cs b/MahApps.Metro/Controls/MetroWindow.cs index e28781b68d..1c0534c853 100644 --- a/MahApps.Metro/Controls/MetroWindow.cs +++ b/MahApps.Metro/Controls/MetroWindow.cs @@ -1,8 +1,6 @@ using System; -using System.Collections.Generic; using System.Collections.ObjectModel; using System.Windows; -using System.Windows.Controls; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; @@ -16,8 +14,6 @@ public class MetroWindow : Window { private const string PART_TitleBar = "PART_TitleBar"; private const string PART_WindowCommands = "PART_WindowCommands"; - private readonly int doubleclick = UnsafeNativeMethods.GetDoubleClickTime(); - private DateTime lastMouseClick; public static readonly DependencyProperty ShowIconOnTitleBarProperty = DependencyProperty.Register("ShowIconOnTitleBar", typeof(bool), typeof(MetroWindow), new PropertyMetadata(true)); public static readonly DependencyProperty ShowTitleBarProperty = DependencyProperty.Register("ShowTitleBar", typeof(bool), typeof(MetroWindow), new PropertyMetadata(true)); @@ -29,9 +25,13 @@ public class MetroWindow : Window public static readonly DependencyProperty SavePositionProperty = DependencyProperty.Register("SaveWindowPosition", typeof(bool), typeof(MetroWindow), new PropertyMetadata(false)); public static readonly DependencyProperty TitleForegroundProperty = DependencyProperty.Register("TitleForeground", typeof(Brush), typeof(MetroWindow)); public static readonly DependencyProperty IgnoreTaskbarOnMaximizeProperty = DependencyProperty.Register("IgnoreTaskbar", typeof(bool), typeof(MetroWindow), new PropertyMetadata(false)); - + + bool isDragging; + public ObservableCollection Flyouts { get; set; } + public WindowCommands WindowCommands { get; set; } + public bool IgnoreTaskbarOnMaximize { get { return (bool)this.GetValue(IgnoreTaskbarOnMaximizeProperty); } @@ -50,18 +50,6 @@ public bool SaveWindowPosition set { SetValue(SavePositionProperty, value); } } - public MetroWindow() - { - if (Flyouts == null) - Flyouts = new ObservableCollection(); - } - static MetroWindow() - { - DefaultStyleKeyProperty.OverrideMetadata(typeof(MetroWindow), new FrameworkPropertyMetadata(typeof(MetroWindow))); - } - - public WindowCommands WindowCommands { get; set; } - public bool ShowIconOnTitleBar { get { return (bool)GetValue(ShowIconOnTitleBarProperty); } @@ -109,6 +97,16 @@ public string WindowTitle get { return TitleCaps ? Title.ToUpper() : Title; } } + public MetroWindow() + { + Flyouts = new ObservableCollection(); + } + + static MetroWindow() + { + DefaultStyleKeyProperty.OverrideMetadata(typeof(MetroWindow), new FrameworkPropertyMetadata(typeof(MetroWindow))); + } + public override void OnApplyTemplate() { base.OnApplyTemplate(); @@ -142,48 +140,55 @@ protected override void OnStateChanged(EventArgs e) protected void TitleBarMouseDown(object sender, MouseButtonEventArgs e) { - if (e.RightButton != MouseButtonState.Pressed && e.MiddleButton != MouseButtonState.Pressed && e.LeftButton == MouseButtonState.Pressed) - DragMove(); + var mousePosition = e.GetPosition(this); + bool isIconClick = ShowIconOnTitleBar && mousePosition.X <= TitlebarHeight && mousePosition.Y <= TitlebarHeight; - if (e.ClickCount == 2 && (ResizeMode == ResizeMode.CanResizeWithGrip || ResizeMode == ResizeMode.CanResize)) + if (e.ChangedButton == MouseButton.Left) { - WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized; - } - } - - protected void TitleBarMouseUp(object sender, MouseButtonEventArgs e) - { - if (!ShowIconOnTitleBar) return; - var mousePosition = GetCorrectPosition(this); - - if (mousePosition.X <= TitlebarHeight && mousePosition.Y <= TitlebarHeight) - { - if ((DateTime.Now - lastMouseClick).TotalMilliseconds <= doubleclick) + if (isIconClick) { - Close(); - return; + if (e.ClickCount == 2) + { + Close(); + } + else + { + ShowSystemMenuPhysicalCoordinates(this, PointToScreen(new Point(0, TitlebarHeight))); + } + } + else + { + if (e.ClickCount == 1) + { + isDragging = true; + DragMove(); + } + else if (e.ClickCount == 2 && (ResizeMode == ResizeMode.CanResizeWithGrip || ResizeMode == ResizeMode.CanResize)) + { + WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized; + } } - lastMouseClick = DateTime.Now; - - ShowSystemMenuPhysicalCoordinates(this, PointToScreen(new Point(0, TitlebarHeight))); } else if (e.ChangedButton == MouseButton.Right) { - ShowSystemMenuPhysicalCoordinates(this, PointToScreen(GetCorrectPosition(this))); + ShowSystemMenuPhysicalCoordinates(this, PointToScreen(mousePosition)); } } - private static Point GetCorrectPosition(Visual relativeTo) + protected void TitleBarMouseUp(object sender, MouseButtonEventArgs e) { - UnsafeNativeMethods.Win32Point w32Mouse; - UnsafeNativeMethods.GetCursorPos(out w32Mouse); - return relativeTo.PointFromScreen(new Point(w32Mouse.X, w32Mouse.Y)); + isDragging = false; } private void TitleBarMouseMove(object sender, MouseEventArgs e) { - if (e.RightButton != MouseButtonState.Pressed && e.MiddleButton != MouseButtonState.Pressed - && e.LeftButton == MouseButtonState.Pressed && WindowState == WindowState.Maximized + if (e.LeftButton != MouseButtonState.Pressed) + { + isDragging = false; + } + + if (isDragging + && WindowState == WindowState.Maximized && ResizeMode != ResizeMode.NoResize) { // Calculating correct left coordinate for multi-screen system.