Skip to content

Commit

Permalink
refactor: remove unused native methods
Browse files Browse the repository at this point in the history
Remove unused GetWindowText and GetWindowBoundingRectangle methods.
  • Loading branch information
punker76 committed Dec 10, 2023
1 parent 28bf413 commit 6fc16f3
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 65 deletions.
1 change: 1 addition & 0 deletions src/MahApps.Metro/Behaviors/WindowsSettingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/MahApps.Metro/Controls/Dialogs/DialogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/MahApps.Metro/Controls/WindowButtonCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using MahApps.Metro.Native;

namespace MahApps.Metro.Controls
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -132,66 +131,5 @@ private static int CalcIntValue(double? value, double fallback)

return (int)fallback;
}

/// <summary> Gets the text associated with the given window handle. </summary>
/// <param name="window"> The window to act on. </param>
/// <returns> The window text. </returns>
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;
}

/// <summary> Gets the text associated with the given window handle. </summary>
/// <param name="window"> The window to act on. </param>
/// <returns> The window text. </returns>
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;
}
}
}
2 changes: 0 additions & 2 deletions src/MahApps.Metro/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ GetKeyNameText
GetMonitorInfo
GetWindowPlacement
GetWindowRect
GetWindowText
GetWindowTextLength
LoadLibrary
LoadString
MapVirtualKey
Expand Down

0 comments on commit 6fc16f3

Please sign in to comment.