ソースを参照

Add setting to hide from alt+tab menu

Closes #48
Daniel Chalmers 3 ヶ月 前
コミット
f2f25ebd52

+ 6 - 0
DesktopClock/MainWindow.xaml.cs

@@ -161,6 +161,11 @@ public partial class MainWindow : Window
 
             case nameof(Settings.Default.ShowInTaskbar):
                 ShowInTaskbar = Settings.Default.ShowInTaskbar;
+                this.SetHiddenFromAltTab(Settings.Default.HideFromAltTab);
+                break;
+
+            case nameof(Settings.Default.HideFromAltTab):
+                this.SetHiddenFromAltTab(Settings.Default.HideFromAltTab);
                 break;
 
             case nameof(Settings.Default.ClickThrough):
@@ -296,6 +301,7 @@ public partial class MainWindow : Window
 
         // Apply click-through setting.
         this.SetClickThrough(Settings.Default.ClickThrough);
+        this.SetHiddenFromAltTab(Settings.Default.HideFromAltTab);
 
         UpdateTimeString();
         _systemClockTimer.Start();

+ 5 - 0
DesktopClock/Properties/Settings.cs

@@ -174,6 +174,11 @@ public sealed class Settings : INotifyPropertyChanged, IDisposable
     /// </summary>
     public bool ShowInTaskbar { get; set; } = false;
 
+    /// <summary>
+    /// Hides the clock window from Alt+Tab.
+    /// </summary>
+    public bool HideFromAltTab { get; set; } = false;
+
     /// <summary>
     /// Height of the clock window.
     /// </summary>

+ 5 - 0
DesktopClock/SettingsWindow.xaml

@@ -118,6 +118,11 @@
                     <TextBlock Text="Show the clock in the Windows taskbar."
                                Style="{StaticResource DescriptionTextBlock}" />
 
+                    <CheckBox Content="Hide from Alt+Tab"
+                              IsChecked="{Binding Settings.HideFromAltTab, Mode=TwoWay}" />
+                    <TextBlock Text="Hide the clock window from the Alt+Tab switcher."
+                               Style="{StaticResource DescriptionTextBlock}" />
+
                     <CheckBox Content="Always on Top"
                               IsChecked="{Binding Settings.Topmost, Mode=TwoWay}" />
                     <TextBlock Text="Keep the clock above other windows."

+ 41 - 0
DesktopClock/Utilities/WindowUtil.cs

@@ -9,6 +9,12 @@ public static class WindowUtil
 {
     private const int GWL_EXSTYLE = -20;
     private const int WS_EX_TRANSPARENT = 0x00000020;
+    private const int WS_EX_TOOLWINDOW = 0x00000080;
+    private const int WS_EX_APPWINDOW = 0x00040000;
+    private const uint SWP_NOMOVE = 0x0002;
+    private const uint SWP_NOSIZE = 0x0001;
+    private const uint SWP_NOZORDER = 0x0004;
+    private const uint SWP_FRAMECHANGED = 0x0020;
 
     [DllImport("user32.dll")]
     private static extern int GetWindowLong(IntPtr hwnd, int index);
@@ -16,6 +22,16 @@ public static class WindowUtil
     [DllImport("user32.dll")]
     private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
 
+    [DllImport("user32.dll")]
+    private static extern bool SetWindowPos(
+        IntPtr hwnd,
+        IntPtr insertAfter,
+        int x,
+        int y,
+        int cx,
+        int cy,
+        uint flags);
+
     /// <summary>
     /// Hides the window until the user opens it again through the taskbar, tray, alt-tab, etc.
     /// </summary>
@@ -48,4 +64,29 @@ public static class WindowUtil
             SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle & ~WS_EX_TRANSPARENT);
         }
     }
+
+    /// <summary>
+    /// Hides or shows the window in Alt+Tab.
+    /// </summary>
+    public static void SetHiddenFromAltTab(this Window window, bool hideFromAltTab)
+    {
+        var hwnd = new WindowInteropHelper(window).Handle;
+        if (hwnd == IntPtr.Zero)
+            return;
+
+        var extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
+        if (hideFromAltTab)
+        {
+            extendedStyle |= WS_EX_TOOLWINDOW;
+            extendedStyle &= ~WS_EX_APPWINDOW;
+        }
+        else
+        {
+            extendedStyle |= WS_EX_APPWINDOW;
+            extendedStyle &= ~WS_EX_TOOLWINDOW;
+        }
+
+        SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle);
+        SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
+    }
 }