Skip to content

Commit 829dbaa

Browse files
committed
Use CSWin32 for code quality
1 parent 5313229 commit 829dbaa

File tree

2 files changed

+17
-32
lines changed

2 files changed

+17
-32
lines changed

Flow.Launcher/Helper/WindowsInteropHelper.cs

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -151,29 +151,11 @@ public static Point TransformPixelsToDIP(Visual visual, double unitX, double uni
151151

152152
#region Alt Tab
153153

154-
private const int GWL_EXSTYLE = -20;
155-
private const int WS_EX_TOOLWINDOW = 0x00000080;
156-
private const int WS_EX_APPWINDOW = 0x00040000;
157-
158-
[DllImport("user32.dll")]
159-
private static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);
160-
161-
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", SetLastError = true)]
162-
private static extern IntPtr IntSetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
163-
164-
[DllImport("user32.dll", EntryPoint = "SetWindowLong", SetLastError = true)]
165-
private static extern int IntSetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
166-
167-
[DllImport("kernel32.dll", EntryPoint = "SetLastError")]
168-
private static extern void SetLastError(int dwErrorCode);
169-
170-
private static IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
154+
private static IntPtr SetWindowLong(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, int dwNewLong)
171155
{
172-
SetLastError(0); // Clear any existing error
173-
174-
if (IntPtr.Size == 4) return new IntPtr(IntSetWindowLong(hWnd, nIndex, IntPtrToInt32(dwNewLong)));
156+
PInvoke.SetLastError(WIN32_ERROR.NO_ERROR); // Clear any existing error
175157

176-
return IntSetWindowLongPtr(hWnd, nIndex, dwNewLong);
158+
return PInvoke.SetWindowLong(hWnd, nIndex, dwNewLong);
177159
}
178160

179161
private static int IntPtrToInt32(IntPtr intPtr)
@@ -182,44 +164,44 @@ private static int IntPtrToInt32(IntPtr intPtr)
182164
}
183165

184166
/// <summary>
185-
/// Hide windows in the Alt+Tab window list
167+
/// Hide windows in the Alt+Tab window list
186168
/// </summary>
187169
/// <param name="window">To hide a window</param>
188170
public static void HideFromAltTab(Window window)
189171
{
190172
var helper = new WindowInteropHelper(window);
191-
var exStyle = GetWindowLong(helper.Handle, GWL_EXSTYLE).ToInt32();
173+
var exStyle = PInvoke.GetWindowLong(new(helper.Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
192174

193175
// Add TOOLWINDOW style, remove APPWINDOW style
194-
exStyle = (exStyle | WS_EX_TOOLWINDOW) & ~WS_EX_APPWINDOW;
176+
var newExStyle = ((uint)exStyle | (uint)WINDOW_EX_STYLE.WS_EX_TOOLWINDOW) & ~(uint)WINDOW_EX_STYLE.WS_EX_APPWINDOW;
195177

196-
SetWindowLong(helper.Handle, GWL_EXSTYLE, new IntPtr(exStyle));
178+
SetWindowLong(new(helper.Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
197179
}
198180

199181
/// <summary>
200-
/// Restore window display in the Alt+Tab window list.
182+
/// Restore window display in the Alt+Tab window list.
201183
/// </summary>
202184
/// <param name="window">To restore the displayed window</param>
203185
public static void ShowInAltTab(Window window)
204186
{
205187
var helper = new WindowInteropHelper(window);
206-
var exStyle = GetWindowLong(helper.Handle, GWL_EXSTYLE).ToInt32();
188+
var exStyle = PInvoke.GetWindowLong(new(helper.Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
207189

208190
// Remove the TOOLWINDOW style and add the APPWINDOW style.
209-
exStyle = (exStyle & ~WS_EX_TOOLWINDOW) | WS_EX_APPWINDOW;
191+
var newExStyle = ((uint)exStyle & ~(uint)WINDOW_EX_STYLE.WS_EX_TOOLWINDOW) | (uint)WINDOW_EX_STYLE.WS_EX_APPWINDOW;
210192

211-
SetWindowLong(helper.Handle, GWL_EXSTYLE, new IntPtr(exStyle));
193+
SetWindowLong(new(helper.Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
212194
}
213195

214196
/// <summary>
215-
/// To obtain the current overridden style of a window.
197+
/// To obtain the current overridden style of a window.
216198
/// </summary>
217199
/// <param name="window">To obtain the style dialog window</param>
218200
/// <returns>current extension style value</returns>
219201
public static int GetCurrentWindowStyle(Window window)
220202
{
221203
var helper = new WindowInteropHelper(window);
222-
return GetWindowLong(helper.Handle, GWL_EXSTYLE).ToInt32();
204+
return PInvoke.GetWindowLong(new(helper.Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
223205
}
224206

225207
#endregion

Flow.Launcher/NativeMethods.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ FindWindowEx
1414
WINDOW_STYLE
1515

1616
WM_ENTERSIZEMOVE
17-
WM_EXITSIZEMOVE
17+
WM_EXITSIZEMOVE
18+
19+
SetLastError
20+
WINDOW_EX_STYLE

0 commit comments

Comments
 (0)