From 6fc16f335605205f91acd9f7ec573f85c2c24189 Mon Sep 17 00:00:00 2001 From: punker76 Date: Sun, 10 Dec 2023 21:38:37 +0100 Subject: [PATCH] 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