Skip to content

Commit 49f1d79

Browse files
committed
Fix SetWindowLong issue
1 parent 7d62ded commit 49f1d79

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

Flow.Launcher.Infrastructure/NativeMethods.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ SystemParametersInfo
2828
SetForegroundWindow
2929

3030
GetWindowLong
31-
SetWindowLong
3231
GetForegroundWindow
3332
GetDesktopWindow
3433
GetShellWindow
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Runtime.InteropServices;
2+
using Windows.Win32.Foundation;
3+
using Windows.Win32.UI.WindowsAndMessaging;
4+
5+
namespace Windows.Win32;
6+
7+
// Edited from: https://github.com/files-community/Files
8+
internal static partial class PInvoke
9+
{
10+
[DllImport("User32", EntryPoint = "SetWindowLongW", ExactSpelling = true)]
11+
static extern int _SetWindowLong(HWND hWnd, int nIndex, int dwNewLong);
12+
13+
[DllImport("User32", EntryPoint = "SetWindowLongPtrW", ExactSpelling = true)]
14+
static extern nint _SetWindowLongPtr(HWND hWnd, int nIndex, nint dwNewLong);
15+
16+
// NOTE:
17+
// CsWin32 doesn't generate SetWindowLong on other than x86 and vice versa.
18+
// For more info, visit https://github.com/microsoft/CsWin32/issues/882
19+
public static unsafe nint SetWindowLongPtr(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, nint dwNewLong)
20+
{
21+
return sizeof(nint) is 4
22+
? _SetWindowLong(hWnd, (int)nIndex, (int)dwNewLong)
23+
: _SetWindowLongPtr(hWnd, (int)nIndex, dwNewLong);
24+
}
25+
}

Flow.Launcher.Infrastructure/Win32Helper.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ public static void HideFromAltTab(Window window)
132132
{
133133
var hwnd = GetWindowHandle(window);
134134

135-
var exStyle = GetCurrentWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
135+
var exStyle = GetWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
136136

137137
// Add TOOLWINDOW style, remove APPWINDOW style
138138
var newExStyle = ((uint)exStyle | (uint)WINDOW_EX_STYLE.WS_EX_TOOLWINDOW) & ~(uint)WINDOW_EX_STYLE.WS_EX_APPWINDOW;
139139

140-
SetWindowLong(hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
140+
SetWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
141141
}
142142

143143
/// <summary>
@@ -148,12 +148,12 @@ public static void ShowInAltTab(Window window)
148148
{
149149
var hwnd = GetWindowHandle(window);
150150

151-
var exStyle = GetCurrentWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
151+
var exStyle = GetWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
152152

153153
// Remove the TOOLWINDOW style and add the APPWINDOW style.
154154
var newExStyle = ((uint)exStyle & ~(uint)WINDOW_EX_STYLE.WS_EX_TOOLWINDOW) | (uint)WINDOW_EX_STYLE.WS_EX_APPWINDOW;
155155

156-
SetWindowLong(GetWindowHandle(window), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
156+
SetWindowStyle(GetWindowHandle(window), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
157157
}
158158

159159
/// <summary>
@@ -164,14 +164,14 @@ public static void DisableControlBox(Window window)
164164
{
165165
var hwnd = GetWindowHandle(window);
166166

167-
var style = GetCurrentWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_STYLE);
167+
var style = GetWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_STYLE);
168168

169169
style &= ~(int)WINDOW_STYLE.WS_SYSMENU;
170170

171-
SetWindowLong(hwnd, WINDOW_LONG_PTR_INDEX.GWL_STYLE, style);
171+
SetWindowStyle(hwnd, WINDOW_LONG_PTR_INDEX.GWL_STYLE, style);
172172
}
173173

174-
private static int GetCurrentWindowStyle(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex)
174+
private static int GetWindowStyle(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex)
175175
{
176176
var style = PInvoke.GetWindowLong(hWnd, nIndex);
177177
if (style == 0 && Marshal.GetLastPInvokeError() != 0)
@@ -181,11 +181,11 @@ private static int GetCurrentWindowStyle(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex
181181
return style;
182182
}
183183

184-
private static int SetWindowLong(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, int dwNewLong)
184+
private static nint SetWindowStyle(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, int dwNewLong)
185185
{
186186
PInvoke.SetLastError(WIN32_ERROR.NO_ERROR); // Clear any existing error
187187

188-
var result = PInvoke.SetWindowLong(hWnd, nIndex, dwNewLong);
188+
var result = PInvoke.SetWindowLongPtr(hWnd, nIndex, dwNewLong);
189189
if (result == 0 && Marshal.GetLastPInvokeError() != 0)
190190
{
191191
throw new Win32Exception(Marshal.GetLastPInvokeError());
@@ -299,14 +299,14 @@ public static Point TransformPixelsToDIP(Visual visual, double unitX, double uni
299299

300300
#region WndProc
301301

302-
public static bool WM_ENTERSIZEMOVE(int msg)
302+
public static bool WM_ENTERSIZEMOVE(uint msg)
303303
{
304-
return msg == (int)PInvoke.WM_ENTERSIZEMOVE;
304+
return msg == PInvoke.WM_ENTERSIZEMOVE;
305305
}
306306

307-
public static bool WM_EXITSIZEMOVE(int msg)
307+
public static bool WM_EXITSIZEMOVE(uint msg)
308308
{
309-
return msg == (int)PInvoke.WM_EXITSIZEMOVE;
309+
return msg == PInvoke.WM_EXITSIZEMOVE;
310310
}
311311

312312
#endregion

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,13 @@ private async void OnContextMenusForSettingsClick(object sender, RoutedEventArgs
377377

378378
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
379379
{
380-
if (Win32Helper.WM_ENTERSIZEMOVE(msg))
380+
if (Win32Helper.WM_ENTERSIZEMOVE((uint)msg))
381381
{
382382
_initialWidth = (int)Width;
383383
_initialHeight = (int)Height;
384384
handled = true;
385385
}
386-
else if (Win32Helper.WM_EXITSIZEMOVE(msg))
386+
else if (Win32Helper.WM_EXITSIZEMOVE((uint)msg))
387387
{
388388
if (_initialHeight != (int)Height)
389389
{

0 commit comments

Comments
 (0)