Skip to content

Commit 0893134

Browse files
Merge pull request #3241 from ZGGSONG/perf_alt_tab_not_show
perf: hide main window from alt tab program switcher
2 parents 502129d + dc07e76 commit 0893134

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

Flow.Launcher/Helper/WindowsInteropHelper.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,64 @@ 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 static int SetWindowLong(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, int dwNewLong)
155+
{
156+
PInvoke.SetLastError(WIN32_ERROR.NO_ERROR); // Clear any existing error
157+
158+
var result = PInvoke.SetWindowLong(hWnd, nIndex, dwNewLong);
159+
if (result == 0 && Marshal.GetLastPInvokeError() != 0)
160+
{
161+
throw new Win32Exception(Marshal.GetLastPInvokeError());
162+
}
163+
164+
return result;
165+
}
166+
167+
/// <summary>
168+
/// Hide windows in the Alt+Tab window list
169+
/// </summary>
170+
/// <param name="window">To hide a window</param>
171+
public static void HideFromAltTab(Window window)
172+
{
173+
var exStyle = GetCurrentWindowStyle(window);
174+
175+
// Add TOOLWINDOW style, remove APPWINDOW style
176+
var newExStyle = ((uint)exStyle | (uint)WINDOW_EX_STYLE.WS_EX_TOOLWINDOW) & ~(uint)WINDOW_EX_STYLE.WS_EX_APPWINDOW;
177+
178+
SetWindowLong(new(new WindowInteropHelper(window).Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
179+
}
180+
181+
/// <summary>
182+
/// Restore window display in the Alt+Tab window list.
183+
/// </summary>
184+
/// <param name="window">To restore the displayed window</param>
185+
public static void ShowInAltTab(Window window)
186+
{
187+
var exStyle = GetCurrentWindowStyle(window);
188+
189+
// Remove the TOOLWINDOW style and add the APPWINDOW style.
190+
var newExStyle = ((uint)exStyle & ~(uint)WINDOW_EX_STYLE.WS_EX_TOOLWINDOW) | (uint)WINDOW_EX_STYLE.WS_EX_APPWINDOW;
191+
192+
SetWindowLong(new(new WindowInteropHelper(window).Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (int)newExStyle);
193+
}
194+
195+
/// <summary>
196+
/// To obtain the current overridden style of a window.
197+
/// </summary>
198+
/// <param name="window">To obtain the style dialog window</param>
199+
/// <returns>current extension style value</returns>
200+
private static int GetCurrentWindowStyle(Window window)
201+
{
202+
var style = PInvoke.GetWindowLong(new(new WindowInteropHelper(window).Handle), WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
203+
if (style == 0 && Marshal.GetLastPInvokeError() != 0)
204+
{
205+
throw new Win32Exception(Marshal.GetLastPInvokeError());
206+
}
207+
return style;
208+
}
209+
210+
#endregion
151211
}

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
}

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)