From 3078b1925c54459c05478587739caf3a68e92878 Mon Sep 17 00:00:00 2001 From: 0x5BFA <62196528+0x5bfa@users.noreply.github.com> Date: Mon, 10 Feb 2025 05:28:24 +0900 Subject: [PATCH] Code Quality: Defined manual SetWindowLongPtr (#16781) --- src/Files.App.CsWin32/NativeMethods.txt | 2 +- src/Files.App.CsWin32/Windows.Win32.Extras.cs | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Files.App.CsWin32/NativeMethods.txt b/src/Files.App.CsWin32/NativeMethods.txt index c521ed9b183b..aa4758a66497 100644 --- a/src/Files.App.CsWin32/NativeMethods.txt +++ b/src/Files.App.CsWin32/NativeMethods.txt @@ -98,7 +98,7 @@ FindWindow SendMessage IsWindowVisible COPYDATASTRUCT -SetWindowLongPtr +WINDOW_LONG_PTR_INDEX GetDpiForWindow CallWindowProc MINMAXINFO diff --git a/src/Files.App.CsWin32/Windows.Win32.Extras.cs b/src/Files.App.CsWin32/Windows.Win32.Extras.cs index 048d8d1f7681..2e3fbe69b4aa 100644 --- a/src/Files.App.CsWin32/Windows.Win32.Extras.cs +++ b/src/Files.App.CsWin32/Windows.Win32.Extras.cs @@ -3,6 +3,7 @@ using System.Runtime.InteropServices; using Windows.Win32.Foundation; +using Windows.Win32.UI.WindowsAndMessaging; namespace Windows.Win32 { @@ -17,4 +18,23 @@ namespace UI.WindowsAndMessaging [UnmanagedFunctionPointer(CallingConvention.Winapi)] public delegate LRESULT WNDPROC(HWND hWnd, uint msg, WPARAM wParam, LPARAM lParam); } + + public static partial class PInvoke + { + [DllImport("User32", EntryPoint = "SetWindowLongW", ExactSpelling = true)] + static extern int _SetWindowLong(HWND hWnd, int nIndex, int dwNewLong); + + [DllImport("User32", EntryPoint = "SetWindowLongPtrW", ExactSpelling = true)] + static extern nint _SetWindowLongPtr(HWND hWnd, int nIndex, nint dwNewLong); + + // NOTE: + // CsWin32 doesn't generate SetWindowLong on other than x86 and vice versa. + // For more info, visit https://github.com/microsoft/CsWin32/issues/882 + public static unsafe nint SetWindowLongPtr(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, nint dwNewLong) + { + return sizeof(nint) is 4 + ? (nint)_SetWindowLong(hWnd, (int)nIndex, (int)dwNewLong) + : _SetWindowLongPtr(hWnd, (int)nIndex, dwNewLong); + } + } }