From d1ec437d5186301b4ca3c3977992fde15bb4a095 Mon Sep 17 00:00:00 2001 From: Konstantin S Date: Thu, 3 Aug 2023 01:54:23 +0400 Subject: [PATCH] feat: Added Window ShowInTaskbar and HideInTaskbar extensions. --- .../H.NotifyIcon.Apps.Maui.csproj | 1 + .../H.NotifyIcon.Shared/WindowExtensions.cs | 36 +++++++++++++++++++ src/libs/H.NotifyIcon/Core/WindowUtilities.cs | 30 ++++++++++++++++ .../H.NotifyIcon/Interop/User32Methods.cs | 8 ++--- 4 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/apps/H.NotifyIcon.Apps.Maui/H.NotifyIcon.Apps.Maui.csproj b/src/apps/H.NotifyIcon.Apps.Maui/H.NotifyIcon.Apps.Maui.csproj index f3a61ba..60bfca8 100644 --- a/src/apps/H.NotifyIcon.Apps.Maui/H.NotifyIcon.Apps.Maui.csproj +++ b/src/apps/H.NotifyIcon.Apps.Maui/H.NotifyIcon.Apps.Maui.csproj @@ -1,6 +1,7 @@  + $(TargetFrameworks);net7.0-maccatalyst $(TargetFrameworks);net7.0-windows10.0.19041.0 diff --git a/src/libs/H.NotifyIcon.Shared/WindowExtensions.cs b/src/libs/H.NotifyIcon.Shared/WindowExtensions.cs index 4992a3e..b685043 100644 --- a/src/libs/H.NotifyIcon.Shared/WindowExtensions.cs +++ b/src/libs/H.NotifyIcon.Shared/WindowExtensions.cs @@ -69,4 +69,40 @@ public static void Show( #pragma warning restore CA1416 // Validate platform compatibility } } + + /// + /// + /// + /// + public static void ShowInTaskbar( + this Window window) + { + window = window ?? throw new ArgumentNullException(nameof(window)); + +#if HAS_WPF + window.ShowInTaskbar = true; +#elif !HAS_UNO && !HAS_MAUI + WindowUtilities.ShowWindowInTaskbar(WindowNative.GetWindowHandle(window)); +#elif HAS_MAUI_WINUI + WindowUtilities.ShowWindowInTaskbar(WindowNative.GetWindowHandle(window.Handler.PlatformView)); +#endif + } + + /// + /// + /// + /// + public static void HideInTaskbar( + this Window window) + { + window = window ?? throw new ArgumentNullException(nameof(window)); + +#if HAS_WPF + window.ShowInTaskbar = false; +#elif !HAS_UNO && !HAS_MAUI + WindowUtilities.HideWindowInTaskbar(WindowNative.GetWindowHandle(window)); +#elif HAS_MAUI_WINUI + WindowUtilities.HideWindowInTaskbar(WindowNative.GetWindowHandle(window.Handler.PlatformView)); +#endif + } } diff --git a/src/libs/H.NotifyIcon/Core/WindowUtilities.cs b/src/libs/H.NotifyIcon/Core/WindowUtilities.cs index 29f37e9..7c8114c 100644 --- a/src/libs/H.NotifyIcon/Core/WindowUtilities.cs +++ b/src/libs/H.NotifyIcon/Core/WindowUtilities.cs @@ -97,7 +97,37 @@ public static bool ShowWindow(nint hWnd) { return PInvoke.ShowWindow(new HWND(hWnd), SHOW_WINDOW_CMD.SW_SHOW); } + + /// + /// + /// + /// + /// + public static void ShowWindowInTaskbar(nint hWnd) + { + var window = new HWND(hWnd); + var style = (WINDOW_EX_STYLE)User32Methods.GetWindowLong(window, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE); + style |= WINDOW_EX_STYLE.WS_EX_APPWINDOW; + style &= ~(WINDOW_EX_STYLE.WS_EX_TOOLWINDOW); + + _ = User32Methods.SetWindowLong(window, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (nint)style); + } + + /// + /// + /// + /// + /// + public static void HideWindowInTaskbar(nint hWnd) + { + var window = new HWND(hWnd); + var style = (WINDOW_EX_STYLE)User32Methods.GetWindowLong(window, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE); + style |= WINDOW_EX_STYLE.WS_EX_TOOLWINDOW; + style &= ~(WINDOW_EX_STYLE.WS_EX_APPWINDOW); + _ = User32Methods.SetWindowLong(window, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, (nint)style); + } + /// /// /// diff --git a/src/libs/H.NotifyIcon/Interop/User32Methods.cs b/src/libs/H.NotifyIcon/Interop/User32Methods.cs index 6450b40..205f754 100644 --- a/src/libs/H.NotifyIcon/Interop/User32Methods.cs +++ b/src/libs/H.NotifyIcon/Interop/User32Methods.cs @@ -9,14 +9,14 @@ internal static class User32Methods internal static nint SetWindowLong(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex, nint dwNewLong) { return Environment.Is64BitProcess - ? PInvoke.SetWindowLongPtr(hWnd, nIndex, dwNewLong) - : PInvoke.SetWindowLong(hWnd, nIndex, (int)dwNewLong); + ? PInvoke.SetWindowLongPtr(hWnd, nIndex, dwNewLong).EnsureNonZero() + : PInvoke.SetWindowLong(hWnd, nIndex, (int)dwNewLong).EnsureNonZero(); } internal static nint GetWindowLong(HWND hWnd, WINDOW_LONG_PTR_INDEX nIndex) { return Environment.Is64BitProcess - ? PInvoke.GetWindowLongPtr(hWnd, nIndex) - : PInvoke.GetWindowLong(hWnd, nIndex); + ? PInvoke.GetWindowLongPtr(hWnd, nIndex).EnsureNonZero() + : PInvoke.GetWindowLong(hWnd, nIndex).EnsureNonZero(); } }