From 6fc16f335605205f91acd9f7ec573f85c2c24189 Mon Sep 17 00:00:00 2001 From: punker76 Date: Sun, 10 Dec 2023 21:38:37 +0100 Subject: [PATCH 1/2] refactor: remove unused native methods Remove unused GetWindowText and GetWindowBoundingRectangle methods. --- .../Behaviors/WindowsSettingBehavior.cs | 1 + .../Controls/Dialogs/DialogManager.cs | 1 + .../Controls/WindowButtonCommands.cs | 1 + .../{Internal => Native}/PInvokeExtensions.cs | 0 .../{Controls => Native}/WinApiHelper.cs | 64 +------------------ src/MahApps.Metro/NativeMethods.txt | 2 - 6 files changed, 4 insertions(+), 65 deletions(-) rename src/MahApps.Metro/{Internal => Native}/PInvokeExtensions.cs (100%) rename src/MahApps.Metro/{Controls => Native}/WinApiHelper.cs (70%) diff --git a/src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs b/src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs index 2ab6ae19fa..2e3b30c1ea 100644 --- a/src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs +++ b/src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs @@ -12,6 +12,7 @@ using Windows.Win32.Graphics.Gdi; using Windows.Win32.UI.WindowsAndMessaging; using MahApps.Metro.Controls; +using MahApps.Metro.Native; using Microsoft.Xaml.Behaviors; namespace MahApps.Metro.Behaviors diff --git a/src/MahApps.Metro/Controls/Dialogs/DialogManager.cs b/src/MahApps.Metro/Controls/Dialogs/DialogManager.cs index 28ba6e0282..a326da9af4 100644 --- a/src/MahApps.Metro/Controls/Dialogs/DialogManager.cs +++ b/src/MahApps.Metro/Controls/Dialogs/DialogManager.cs @@ -8,6 +8,7 @@ using System.Windows; using System.Windows.Controls; using ControlzEx.Theming; +using MahApps.Metro.Native; using MahApps.Metro.ValueBoxes; namespace MahApps.Metro.Controls.Dialogs diff --git a/src/MahApps.Metro/Controls/WindowButtonCommands.cs b/src/MahApps.Metro/Controls/WindowButtonCommands.cs index 3045744901..d5b4631619 100644 --- a/src/MahApps.Metro/Controls/WindowButtonCommands.cs +++ b/src/MahApps.Metro/Controls/WindowButtonCommands.cs @@ -7,6 +7,7 @@ using System.Windows.Controls; using System.Windows.Input; using System.Windows.Threading; +using MahApps.Metro.Native; namespace MahApps.Metro.Controls { diff --git a/src/MahApps.Metro/Internal/PInvokeExtensions.cs b/src/MahApps.Metro/Native/PInvokeExtensions.cs similarity index 100% rename from src/MahApps.Metro/Internal/PInvokeExtensions.cs rename to src/MahApps.Metro/Native/PInvokeExtensions.cs diff --git a/src/MahApps.Metro/Controls/WinApiHelper.cs b/src/MahApps.Metro/Native/WinApiHelper.cs similarity index 70% rename from src/MahApps.Metro/Controls/WinApiHelper.cs rename to src/MahApps.Metro/Native/WinApiHelper.cs index 21a0347c6f..e494dbcfeb 100644 --- a/src/MahApps.Metro/Controls/WinApiHelper.cs +++ b/src/MahApps.Metro/Native/WinApiHelper.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System; -using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; @@ -15,7 +14,7 @@ using Windows.Win32.Graphics.Gdi; using Windows.Win32.UI.WindowsAndMessaging; -namespace MahApps.Metro.Controls +namespace MahApps.Metro.Native { internal static class WinApiHelper { @@ -132,66 +131,5 @@ private static int CalcIntValue(double? value, double fallback) return (int)fallback; } - - /// Gets the text associated with the given window handle. - /// The window to act on. - /// The window text. - internal static string? GetWindowText(this Window? window) - { - if (window is null == false - && PresentationSource.FromVisual(window) is HwndSource source - && source.IsDisposed == false - && source.RootVisual is null == false - && source.Handle != IntPtr.Zero) - { - int bufferSize = PInvoke.GetWindowTextLength(new HWND(source.Handle)) + 1; - unsafe - { - fixed (char* windowNameChars = new char[bufferSize]) - { - PInvoke.GetWindowText(new HWND(source.Handle), windowNameChars, bufferSize); - return new string(windowNameChars); - } - } - } - - return default; - } - - /// Gets the text associated with the given window handle. - /// The window to act on. - /// The window text. - internal static Rect GetWindowBoundingRectangle(this Window? window) - { - var bounds = new Rect(0, 0, 0, 0); - - if (window is null == false - && PresentationSource.FromVisual(window) is HwndSource source - && source.IsDisposed == false - && source.RootVisual is null == false - && source.Handle != IntPtr.Zero) - { - var rc = new RECT(); - - try - { - unsafe - { - PInvoke.GetWindowRect((HWND)source.Handle, &rc); - } - } - // Allow empty catch statements. -#pragma warning disable 56502 - catch (Win32Exception) - { - } - // Disallow empty catch statements. -#pragma warning restore 56502 - - bounds = new Rect(rc.left, rc.top, rc.GetWidth(), rc.GetHeight()); - } - - return bounds; - } } } \ No newline at end of file diff --git a/src/MahApps.Metro/NativeMethods.txt b/src/MahApps.Metro/NativeMethods.txt index 931a6ec862..a596d2f993 100644 --- a/src/MahApps.Metro/NativeMethods.txt +++ b/src/MahApps.Metro/NativeMethods.txt @@ -2,8 +2,6 @@ GetKeyNameText GetMonitorInfo GetWindowPlacement GetWindowRect -GetWindowText -GetWindowTextLength LoadLibrary LoadString MapVirtualKey From 5ea6fd1ff9b4c80f3c8e5f35fad7607620d04249 Mon Sep 17 00:00:00 2001 From: punker76 Date: Sun, 10 Dec 2023 21:39:54 +0100 Subject: [PATCH 2/2] feat: #4445 add new property CloseOnIconDoubleClick Add new property CloseOnIconDoubleClick to allow disable closing window if the user double clicks on window icon. --- src/MahApps.Metro/Controls/MetroWindow.cs | 37 +++++++++++++++-------- 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/MahApps.Metro/Controls/MetroWindow.cs b/src/MahApps.Metro/Controls/MetroWindow.cs index 590d153d22..a683aa804f 100644 --- a/src/MahApps.Metro/Controls/MetroWindow.cs +++ b/src/MahApps.Metro/Controls/MetroWindow.cs @@ -159,6 +159,22 @@ public MultiFrameImageMode IconScalingMode set => this.SetValue(IconScalingModeProperty, value); } + /// Identifies the dependency property. + public static readonly DependencyProperty CloseOnIconDoubleClickProperty + = DependencyProperty.Register(nameof(CloseOnIconDoubleClick), + typeof(bool), + typeof(MetroWindow), + new PropertyMetadata(BooleanBoxes.TrueBox)); + + /// + /// Gets or sets the value to close the window if the user double click on the window icon. + /// + public bool CloseOnIconDoubleClick + { + get => (bool)this.GetValue(CloseOnIconDoubleClickProperty); + set => this.SetValue(CloseOnIconDoubleClickProperty, BooleanBoxes.Box(value)); + } + /// Identifies the dependency property. public static readonly DependencyProperty ShowTitleBarProperty = DependencyProperty.Register(nameof(ShowTitleBar), @@ -1479,7 +1495,7 @@ private void ClearWindowEvents() if (this.icon != null) { - this.icon.MouseDown -= this.IconMouseDown; + this.icon.MouseLeftButtonDown -= this.OnIconMouseLeftButtonDown; } this.SizeChanged -= this.MetroWindow_SizeChanged; @@ -1493,7 +1509,7 @@ private void SetWindowEvents() // set mouse down/up for icon if (this.icon != null && this.icon.Visibility == Visibility.Visible) { - this.icon.MouseDown += this.IconMouseDown; + this.icon.MouseDown += this.OnIconMouseLeftButtonDown; } if (this.windowTitleThumb != null) @@ -1527,20 +1543,17 @@ private void SetWindowEvents() } } - private void IconMouseDown(object sender, MouseButtonEventArgs e) + private void OnIconMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { - if (e.ChangedButton == MouseButton.Left) + if (e.ClickCount == 2 && this.CloseOnIconDoubleClick) + { + this.Close(); + } + else if (this.ShowSystemMenu) { - if (e.ClickCount == 2) - { - this.Close(); - } - else if (this.ShowSystemMenu) - { #pragma warning disable 618 - ControlzEx.SystemCommands.ShowSystemMenuPhysicalCoordinates(this, this.PointToScreen(new Point(this.BorderThickness.Left, this.TitleBarHeight + this.BorderThickness.Top))); + ControlzEx.SystemCommands.ShowSystemMenuPhysicalCoordinates(this, this.PointToScreen(new Point(this.BorderThickness.Left, this.TitleBarHeight + this.BorderThickness.Top))); #pragma warning restore 618 - } } }