diff --git a/Flow.Launcher.Infrastructure/NativeMethods.txt b/Flow.Launcher.Infrastructure/NativeMethods.txt index 5d72239f3..f080f24de 100644 --- a/Flow.Launcher.Infrastructure/NativeMethods.txt +++ b/Flow.Launcher.Infrastructure/NativeMethods.txt @@ -28,7 +28,6 @@ SystemParametersInfo SetForegroundWindow GetWindowLong -SetWindowLong GetForegroundWindow GetDesktopWindow GetShellWindow diff --git a/Flow.Launcher.Infrastructure/PInvokeExtensions.cs b/Flow.Launcher.Infrastructure/PInvokeExtensions.cs new file mode 100644 index 000000000..1a72ab7a6 --- /dev/null +++ b/Flow.Launcher.Infrastructure/PInvokeExtensions.cs @@ -0,0 +1,25 @@ +using System.Runtime.InteropServices; +using Windows.Win32.Foundation; +using Windows.Win32.UI.WindowsAndMessaging; + +namespace Windows.Win32; + +// Edited from: https://github.com/files-community/Files +internal 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 + ? _SetWindowLong(hWnd, (int)nIndex, (int)dwNewLong) + : _SetWindowLongPtr(hWnd, (int)nIndex, dwNewLong); + } +} diff --git a/Flow.Launcher.Infrastructure/Win32Helper.cs b/Flow.Launcher.Infrastructure/Win32Helper.cs index 6e429cddd..cdb384ab7 100644 --- a/Flow.Launcher.Infrastructure/Win32Helper.cs +++ b/Flow.Launcher.Infrastructure/Win32Helper.cs @@ -132,12 +132,12 @@ namespace Flow.Launcher.Infrastructure { var hwnd = GetWindowHandle(window); - var exStyle = GetCurrentWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE); + var exStyle = GetWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE); // Add TOOLWINDOW style, remove APPWINDOW style var newExStyle = ((uint)exStyle | (uint)WINDOW_EX_STYLE.WS_EX_TOOLWINDOW) & ~(uint)WINDOW_EX_STYLE.WS_EX_APPWINDOW; - SetWindowLong(hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle); + SetWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle); } /// @@ -148,12 +148,12 @@ namespace Flow.Launcher.Infrastructure { var hwnd = GetWindowHandle(window); - var exStyle = GetCurrentWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE); + var exStyle = GetWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE); // Remove the TOOLWINDOW style and add the APPWINDOW style. var newExStyle = ((uint)exStyle & ~(uint)WINDOW_EX_STYLE.WS_EX_TOOLWINDOW) | (uint)WINDOW_EX_STYLE.WS_EX_APPWINDOW; - SetWindowLong(GetWindowHandle(window), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle); + SetWindowStyle(GetWindowHandle(window), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle); } /// @@ -164,14 +164,14 @@ namespace Flow.Launcher.Infrastructure { var hwnd = GetWindowHandle(window); - var style = GetCurrentWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_STYLE); + var style = GetWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_STYLE); style &= ~(int)WINDOW_STYLE.WS_SYSMENU; - SetWindowLong(hwnd, WINDOW_LONG_PTR_INDEX.GWL_STYLE, style); + SetWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_STYLE, style); } - private static int GetCurrentWindowStyle(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex) + private static int GetWindowStyle(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex) { var style = PInvoke.GetWindowLong(hWnd, nIndex); if (style == 0 && Marshal.GetLastPInvokeError() != 0) @@ -181,11 +181,11 @@ namespace Flow.Launcher.Infrastructure return style; } - private static int SetWindowLong(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, int dwNewLong) + private static nint SetWindowStyle(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, int dwNewLong) { PInvoke.SetLastError(WIN32_ERROR.NO_ERROR); // Clear any existing error - var result = PInvoke.SetWindowLong(hWnd, nIndex, dwNewLong); + var result = PInvoke.SetWindowLongPtr(hWnd, nIndex, dwNewLong); if (result == 0 && Marshal.GetLastPInvokeError() != 0) { throw new Win32Exception(Marshal.GetLastPInvokeError()); @@ -299,14 +299,14 @@ namespace Flow.Launcher.Infrastructure #region WndProc - public static bool WM_ENTERSIZEMOVE(int msg) + public static bool WM_ENTERSIZEMOVE(uint msg) { - return msg == (int)PInvoke.WM_ENTERSIZEMOVE; + return msg == PInvoke.WM_ENTERSIZEMOVE; } - public static bool WM_EXITSIZEMOVE(int msg) + public static bool WM_EXITSIZEMOVE(uint msg) { - return msg == (int)PInvoke.WM_EXITSIZEMOVE; + return msg == PInvoke.WM_EXITSIZEMOVE; } #endregion diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index d0375e8f6..9038762f2 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -377,13 +377,13 @@ namespace Flow.Launcher private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { - if (Win32Helper.WM_ENTERSIZEMOVE(msg)) + if (Win32Helper.WM_ENTERSIZEMOVE((uint)msg)) { _initialWidth = (int)Width; _initialHeight = (int)Height; handled = true; } - else if (Win32Helper.WM_EXITSIZEMOVE(msg)) + else if (Win32Helper.WM_EXITSIZEMOVE((uint)msg)) { if (_initialHeight != (int)Height) {