Skip to content

Commit 5313229

Browse files
committed
perf: hide main window from alt tab program switcher #2356
1 parent 502129d commit 5313229

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

Flow.Launcher/Helper/WindowsInteropHelper.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,79 @@ public static Point TransformPixelsToDIP(Visual visual, double unitX, double uni
148148

149149
return new Point((int)(matrix.M11 * unitX), (int)(matrix.M22 * unitY));
150150
}
151+
152+
#region Alt Tab
153+
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)
171+
{
172+
SetLastError(0); // Clear any existing error
173+
174+
if (IntPtr.Size == 4) return new IntPtr(IntSetWindowLong(hWnd, nIndex, IntPtrToInt32(dwNewLong)));
175+
176+
return IntSetWindowLongPtr(hWnd, nIndex, dwNewLong);
177+
}
178+
179+
private static int IntPtrToInt32(IntPtr intPtr)
180+
{
181+
return unchecked((int)intPtr.ToInt64());
182+
}
183+
184+
/// <summary>
185+
/// Hide windows in the Alt+Tab window list
186+
/// </summary>
187+
/// <param name="window">To hide a window</param>
188+
public static void HideFromAltTab(Window window)
189+
{
190+
var helper = new WindowInteropHelper(window);
191+
var exStyle = GetWindowLong(helper.Handle, GWL_EXSTYLE).ToInt32();
192+
193+
// Add TOOLWINDOW style, remove APPWINDOW style
194+
exStyle = (exStyle | WS_EX_TOOLWINDOW) & ~WS_EX_APPWINDOW;
195+
196+
SetWindowLong(helper.Handle, GWL_EXSTYLE, new IntPtr(exStyle));
197+
}
198+
199+
/// <summary>
200+
/// Restore window display in the Alt+Tab window list.
201+
/// </summary>
202+
/// <param name="window">To restore the displayed window</param>
203+
public static void ShowInAltTab(Window window)
204+
{
205+
var helper = new WindowInteropHelper(window);
206+
var exStyle = GetWindowLong(helper.Handle, GWL_EXSTYLE).ToInt32();
207+
208+
// Remove the TOOLWINDOW style and add the APPWINDOW style.
209+
exStyle = (exStyle & ~WS_EX_TOOLWINDOW) | WS_EX_APPWINDOW;
210+
211+
SetWindowLong(helper.Handle, GWL_EXSTYLE, new IntPtr(exStyle));
212+
}
213+
214+
/// <summary>
215+
/// To obtain the current overridden style of a window.
216+
/// </summary>
217+
/// <param name="window">To obtain the style dialog window</param>
218+
/// <returns>current extension style value</returns>
219+
public static int GetCurrentWindowStyle(Window window)
220+
{
221+
var helper = new WindowInteropHelper(window);
222+
return GetWindowLong(helper.Handle, GWL_EXSTYLE).ToInt32();
223+
}
224+
225+
#endregion
151226
}

Flow.Launcher/MainWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
Closing="OnClosing"
2121
Deactivated="OnDeactivated"
2222
Icon="Images/app.png"
23+
SourceInitialized="OnSourceInitialized"
2324
Initialized="OnInitialized"
2425
Left="{Binding Settings.WindowLeft, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
2526
Loaded="OnLoaded"

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ private async void OnClosing(object sender, CancelEventArgs e)
171171
Environment.Exit(0);
172172
}
173173

174+
private void OnSourceInitialized(object sender, EventArgs e)
175+
{
176+
WindowsInteropHelper.HideFromAltTab(this);
177+
}
178+
174179
private void OnInitialized(object sender, EventArgs e)
175180
{
176181
}

0 commit comments

Comments
 (0)