Skip to content
Prev Previous commit
Next Next commit
Convert User32 to CsWin32.
  • Loading branch information
Nuklon committed Nov 10, 2025
commit bcd61055a9f3f2ae976a66b67feac7596fedf828
30 changes: 16 additions & 14 deletions src/Wpf.Ui/Controls/ClientAreaBorder/ClientAreaBorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// All Rights Reserved.

using System.Windows.Shell;
using Windows.Win32;
using Windows.Win32.UI.WindowsAndMessaging;
using Wpf.Ui.Appearance;
using Wpf.Ui.Hardware;
using Wpf.Ui.Interop;
Expand Down Expand Up @@ -50,12 +52,12 @@ public class ClientAreaBorder : System.Windows.Controls.Border, IThemeControl
private static Thickness? _resizeFrameBorderThickness;
private static Thickness? _windowChromeNonClientFrameThickness;
private bool _borderBrushApplied = false;
private System.Windows.Window? _oldWindow;
private Window? _oldWindow;

public ApplicationTheme ApplicationTheme { get; set; } = ApplicationTheme.Unknown;

/// <summary>
/// Gets the system value for the padded border thickness (<see cref="User32.SM.CXPADDEDBORDER"/>) in WPF units.
/// Gets the system value for the padded border thickness in WPF units.
/// </summary>
public Thickness PaddedBorderThickness
{
Expand All @@ -66,7 +68,7 @@ public Thickness PaddedBorderThickness
return _paddedBorderThickness.Value;
}

var paddedBorder = Interop.User32.GetSystemMetrics(Interop.User32.SM.CXPADDEDBORDER);
var paddedBorder = PInvoke.GetSystemMetrics(SYSTEM_METRICS_INDEX.SM_CXPADDEDBORDER);

(double factorX, double factorY) = GetDpi();

Expand All @@ -85,7 +87,7 @@ public Thickness PaddedBorderThickness
}

/// <summary>
/// Gets the system <see cref="User32.SM.CXFRAME"/> and <see cref="User32.SM.CYFRAME"/> values in WPF units.
/// Gets the system <see cref="SYSTEM_METRICS_INDEX.SM_CXFRAME"/> and <see cref="SYSTEM_METRICS_INDEX.SM_CYFRAME"/> values in WPF units.
/// </summary>
public static Thickness ResizeFrameBorderThickness =>
_resizeFrameBorderThickness ??= new Thickness(
Expand All @@ -101,20 +103,20 @@ public Thickness PaddedBorderThickness
/// <remarks>
/// If you use a <see cref="WindowChrome"/> to extend the client area of a window to the non-client area, you need to handle the edge margin issue when the window is maximized.
/// Use this property to get the correct margin value when the window is maximized, so that when the window is maximized, the client area can completely cover the screen client area by no less than a single pixel at any DPI.
/// The<see cref="User32.GetSystemMetrics"/> method cannot obtain this value directly.
/// The<see cref="PInvoke.GetSystemMetrics"/> method cannot obtain this value directly.
/// </remarks>
public Thickness WindowChromeNonClientFrameThickness =>
_windowChromeNonClientFrameThickness ??= new Thickness(
ClientAreaBorder.ResizeFrameBorderThickness.Left + PaddedBorderThickness.Left,
ClientAreaBorder.ResizeFrameBorderThickness.Top + PaddedBorderThickness.Top,
ClientAreaBorder.ResizeFrameBorderThickness.Right + PaddedBorderThickness.Right,
ClientAreaBorder.ResizeFrameBorderThickness.Bottom + PaddedBorderThickness.Bottom
ResizeFrameBorderThickness.Left + PaddedBorderThickness.Left,
ResizeFrameBorderThickness.Top + PaddedBorderThickness.Top,
ResizeFrameBorderThickness.Right + PaddedBorderThickness.Right,
ResizeFrameBorderThickness.Bottom + PaddedBorderThickness.Bottom
);

public ClientAreaBorder()
{
ApplicationTheme = Appearance.ApplicationThemeManager.GetAppTheme();
Appearance.ApplicationThemeManager.Changed += OnThemeChanged;
ApplicationTheme = ApplicationThemeManager.GetAppTheme();
ApplicationThemeManager.Changed += OnThemeChanged;
}

private void OnThemeChanged(ApplicationTheme currentApplicationTheme, Color systemAccent)
Expand All @@ -140,7 +142,7 @@ protected override void OnVisualParentChanged(DependencyObject oldParent)
oldWindow.Closing -= OnWindowClosing;
}

var newWindow = (System.Windows.Window?)System.Windows.Window.GetWindow(this);
Window? newWindow = Window.GetWindow(this);

if (newWindow is not null)
{
Expand All @@ -161,7 +163,7 @@ protected override void OnVisualParentChanged(DependencyObject oldParent)

private void OnWindowClosing(object? sender, CancelEventArgs e)
{
Appearance.ApplicationThemeManager.Changed -= OnThemeChanged;
ApplicationThemeManager.Changed -= OnThemeChanged;
if (_oldWindow != null)
{
_oldWindow.Closing -= OnWindowClosing;
Expand All @@ -170,7 +172,7 @@ private void OnWindowClosing(object? sender, CancelEventArgs e)

private void OnWindowStateChanged(object? sender, EventArgs e)
{
if (sender is not System.Windows.Window window)
if (sender is not Window window)
{
return;
}
Expand Down
27 changes: 12 additions & 15 deletions src/Wpf.Ui/Controls/TitleBar/TitleBar.WindowResize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System.ComponentModel;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Shell;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.WindowsAndMessaging;
using Wpf.Ui.Interop;
using Wpf.Ui.Interop.WinDef;
using RECT = Windows.Win32.Foundation.RECT;

// ReSharper disable once CheckNamespace
namespace Wpf.Ui.Controls;
Expand All @@ -28,7 +25,7 @@ namespace Wpf.Ui.Controls;
/// - The implementation prefers the <see cref="System.Windows.Shell.WindowChrome.ResizeBorderThickness"/>
/// value (expressed in device-independent units) when available and translates it into physical pixels;
/// - If WindowChrome or DPI information is not available, the code falls back to system metrics via
/// <see cref="Wpf.Ui.Interop.User32.GetSystemMetrics"/>;
/// <see cref="PInvoke.GetSystemMetrics"/>;
/// - Because <c>WM_NCHITTEST</c> is raised frequently, computed border pixel sizes are cached to
/// reduce overhead; the cache is invalidated when DPI or relevant system parameters change;
/// - This component only augments the <see cref="TitleBar"/> control's non-client hit-testing to
Expand All @@ -49,7 +46,7 @@ public partial class TitleBar

private IntPtr GetWindowBorderHitTestResult(IntPtr hwnd, IntPtr lParam)
{
if (!User32.GetWindowRect(hwnd, out RECT windowRect))
if (!PInvoke.GetWindowRect(new HWND(hwnd), out RECT windowRect))
{
return (IntPtr)User32.WM_NCHITTEST.HTNOWHERE;
}
Expand All @@ -67,13 +64,13 @@ private IntPtr GetWindowBorderHitTestResult(IntPtr hwnd, IntPtr lParam)
uint hit = 0u;

#pragma warning disable
if (x < windowRect.Left + _borderX)
if (x < windowRect.left + _borderX)
hit |= 0b0001u; // left
if (x >= windowRect.Right - _borderX)
if (x >= windowRect.right - _borderX)
hit |= 0b0010u; // right
if (y < windowRect.Top + _borderY)
if (y < windowRect.top + _borderY)
hit |= 0b0100u; // top
if (y >= windowRect.Bottom - _borderY)
if (y >= windowRect.bottom - _borderY)
hit |= 0b1000u; // bottom
#pragma warning restore

Expand Down Expand Up @@ -265,11 +262,11 @@ private static bool TryGetFromSystemMetrics(out int borderX, out int borderY)
try
{
int sx =
User32.GetSystemMetrics(User32.SM.CXSIZEFRAME)
+ User32.GetSystemMetrics(User32.SM.CXPADDEDBORDER);
PInvoke.GetSystemMetrics(SYSTEM_METRICS_INDEX.SM_CXSIZEFRAME)
+ PInvoke.GetSystemMetrics(SYSTEM_METRICS_INDEX.SM_CXPADDEDBORDER);
int sy =
User32.GetSystemMetrics(User32.SM.CYSIZEFRAME)
+ User32.GetSystemMetrics(User32.SM.CXPADDEDBORDER);
PInvoke.GetSystemMetrics(SYSTEM_METRICS_INDEX.SM_CYSIZEFRAME)
+ PInvoke.GetSystemMetrics(SYSTEM_METRICS_INDEX.SM_CXPADDEDBORDER);

borderX = Math.Max(2, sx);
borderY = Math.Max(2, sy);
Expand Down
23 changes: 13 additions & 10 deletions src/Wpf.Ui/Controls/Window/WindowBackdrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
// All Rights Reserved.

using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.Foundation;
using Wpf.Ui.Appearance;
using Wpf.Ui.Interop;
using HRESULT = Wpf.Ui.Interop.HRESULT;

// ReSharper disable once CheckNamespace
namespace Wpf.Ui.Controls;
Expand Down Expand Up @@ -38,7 +41,7 @@ public static bool IsSupported(WindowBackdropType backdropType)
/// <param name="window">The window to which the backdrop effect will be applied.</param>
/// <param name="backdropType">The type of backdrop effect to apply. Determines the visual appearance of the window's backdrop.</param>
/// <returns><see langword="true"/> if the operation was successful; otherwise, <see langword="false"/>.</returns>
public static bool ApplyBackdrop(System.Windows.Window? window, WindowBackdropType backdropType)
public static bool ApplyBackdrop(Window? window, WindowBackdropType backdropType)
{
if (window is null)
{
Expand All @@ -60,7 +63,7 @@ public static bool ApplyBackdrop(System.Windows.Window? window, WindowBackdropTy
window.Loaded += (sender, _1) =>
{
IntPtr windowHandle =
new WindowInteropHelper(sender as System.Windows.Window ?? null)?.Handle ?? IntPtr.Zero;
new WindowInteropHelper(sender as Window)?.Handle ?? IntPtr.Zero;

if (windowHandle == IntPtr.Zero)
{
Expand All @@ -86,7 +89,7 @@ public static bool ApplyBackdrop(IntPtr hWnd, WindowBackdropType backdropType)
return false;
}

if (!User32.IsWindow(hWnd))
if (!PInvoke.IsWindow(new HWND(hWnd)))
{
return false;
}
Expand Down Expand Up @@ -130,7 +133,7 @@ public static bool ApplyBackdrop(IntPtr hWnd, WindowBackdropType backdropType)
/// Tries to remove backdrop effects if they have been applied to the <see cref="Window"/>.
/// </summary>
/// <param name="window">The window from which the effect should be removed.</param>
public static bool RemoveBackdrop(System.Windows.Window? window)
public static bool RemoveBackdrop(Window? window)
{
if (window is null)
{
Expand Down Expand Up @@ -160,7 +163,7 @@ public static bool RemoveBackdrop(IntPtr hWnd)
return false;
}

if (!User32.IsWindow(hWnd))
if (!PInvoke.IsWindow(new HWND(hWnd)))
{
return false;
}
Expand Down Expand Up @@ -190,7 +193,7 @@ public static bool RemoveBackdrop(IntPtr hWnd)
/// </summary>
/// <param name="window">Window to manipulate.</param>
/// <returns><see langword="true"/> if operation was successful.</returns>
public static bool RemoveBackground(System.Windows.Window? window)
public static bool RemoveBackground(Window? window)
{
if (window is null)
{
Expand Down Expand Up @@ -218,7 +221,7 @@ public static bool RemoveBackground(System.Windows.Window? window)
return true;
}

public static bool RemoveTitlebarBackground(System.Windows.Window? window)
public static bool RemoveTitlebarBackground(Window? window)
{
if (window is null)
{
Expand Down Expand Up @@ -259,7 +262,7 @@ private static bool ApplyDwmwWindowAttrubute(IntPtr hWnd, Dwmapi.DWMSBT dwmSbt)
return false;
}

if (!User32.IsWindow(hWnd))
if (!PInvoke.IsWindow(new HWND(hWnd)))
{
return false;
}
Expand Down Expand Up @@ -303,7 +306,7 @@ private static bool RestoreContentBackground(IntPtr hWnd)
return false;
}

if (!User32.IsWindow(hWnd))
if (!PInvoke.IsWindow(new HWND(hWnd)))
{
return false;
}
Expand All @@ -316,7 +319,7 @@ private static bool RestoreContentBackground(IntPtr hWnd)
windowSource.CompositionTarget.BackgroundColor = SystemColors.WindowColor;
}

if (windowSource?.RootVisual is System.Windows.Window window)
if (windowSource?.RootVisual is Window window)
{
var backgroundBrush = window.Resources["ApplicationBackgroundBrush"];

Expand Down
28 changes: 14 additions & 14 deletions src/Wpf.Ui/Interop/UnsafeNativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static bool ApplyWindowCornerPreference(IntPtr handle, WindowCornerPrefer
return false;
}

if (!User32.IsWindow(handle))
if (!PInvoke.IsWindow(new HWND(handle)))
{
return false;
}
Expand Down Expand Up @@ -103,7 +103,7 @@ public static bool ApplyBorderColor(IntPtr handle, int color)
return false;
}

if (!User32.IsWindow(handle))
if (!PInvoke.IsWindow(new HWND(handle)))
{
return false;
}
Expand Down Expand Up @@ -159,7 +159,7 @@ public static bool RemoveWindowDarkMode(IntPtr handle)
return false;
}

if (!User32.IsWindow(handle))
if (!PInvoke.IsWindow(new HWND(handle)))
{
return false;
}
Expand Down Expand Up @@ -196,7 +196,7 @@ public static bool ApplyWindowDarkMode(IntPtr handle)
return false;
}

if (!User32.IsWindow(handle))
if (!PInvoke.IsWindow(new HWND(handle)))
{
return false;
}
Expand Down Expand Up @@ -251,7 +251,7 @@ public static bool RemoveWindowTitlebarContents(IntPtr handle)
return false;
}

if (!User32.IsWindow(handle))
if (!PInvoke.IsWindow(new HWND(handle)))
{
return false;
}
Expand All @@ -277,7 +277,7 @@ public static bool ApplyWindowBackdrop(IntPtr handle, WindowBackdropType backgro
return false;
}

if (!User32.IsWindow(handle))
if (!PInvoke.IsWindow(new HWND(handle)))
{
return false;
}
Expand All @@ -304,7 +304,7 @@ public static bool ApplyWindowBackdrop(IntPtr handle, WindowBackdropType backgro
/// <param name="backdropType">Background backdrop type.</param>
public static bool IsWindowHasBackdrop(IntPtr handle, WindowBackdropType backdropType)
{
if (!User32.IsWindow(handle))
if (!PInvoke.IsWindow(new HWND(handle)))
{
return false;
}
Expand Down Expand Up @@ -334,7 +334,7 @@ public static bool IsWindowHasLegacyMica(Window? window) =>
/// <param name="handle">Window handle.</param>
public static bool IsWindowHasLegacyMica(IntPtr handle)
{
if (!User32.IsWindow(handle))
if (!PInvoke.IsWindow(new HWND(handle)))
{
return false;
}
Expand Down Expand Up @@ -457,7 +457,7 @@ internal static bool SetTaskbarState(IntPtr hWnd, ShObjIdl.TBPFLAG taskbarFlag)
return false;
}

if (!User32.IsWindow(hWnd))
if (!PInvoke.IsWindow(new HWND(hWnd)))
{
return false;
}
Expand Down Expand Up @@ -488,7 +488,7 @@ internal static bool SetTaskbarValue(IntPtr hWnd, ShObjIdl.TBPFLAG taskbarFlag,
return false;
}

if (!User32.IsWindow(hWnd))
if (!PInvoke.IsWindow(new HWND(hWnd)))
{
return false;
}
Expand Down Expand Up @@ -530,7 +530,7 @@ public static unsafe bool RemoveWindowCaption(IntPtr hWnd)
return false;
}

if (!User32.IsWindow(hWnd))
if (!PInvoke.IsWindow(new HWND(hWnd)))
{
return false;
}
Expand Down Expand Up @@ -571,7 +571,7 @@ public static unsafe bool ExtendClientAreaIntoTitleBar(IntPtr hWnd)
return false;
}

if (!User32.IsWindow(hWnd))
if (!PInvoke.IsWindow(new HWND(hWnd)))
{
return false;
}
Expand Down Expand Up @@ -612,7 +612,7 @@ public static unsafe bool ExtendClientAreaIntoTitleBar(IntPtr hWnd)
PInvoke.DwmExtendFrameIntoClientArea(new HWND(hWnd), dwmMargin);

// #4 Clear rounding region
Interop.User32.SetWindowRgn(hWnd, IntPtr.Zero, PInvoke.IsWindowVisible(new HWND(hWnd)));
PInvoke.SetWindowRgn(new HWND(hWnd), null, PInvoke.IsWindowVisible(new HWND(hWnd)));

return true;
}
Expand All @@ -630,7 +630,7 @@ public static bool IsCompositionEnabled()
/// </summary>
public static bool IsValidWindow(IntPtr hWnd)
{
return User32.IsWindow(hWnd);
return PInvoke.IsWindow(new HWND(hWnd));
}

/// <summary>
Expand Down
Loading