Skip to content

Commit

Permalink
feat: Added Window ShowInTaskbar and HideInTaskbar extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Aug 2, 2023
1 parent c937d25 commit d1ec437
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(TargetFrameworks);net7.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net7.0-tizen</TargetFrameworks> -->
Expand Down
36 changes: 36 additions & 0 deletions src/libs/H.NotifyIcon.Shared/WindowExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,40 @@ public static void Show(
#pragma warning restore CA1416 // Validate platform compatibility
}
}

/// <summary>
///
/// </summary>
/// <returns></returns>
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
}

/// <summary>
///
/// </summary>
/// <returns></returns>
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
}
}
30 changes: 30 additions & 0 deletions src/libs/H.NotifyIcon/Core/WindowUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,37 @@ public static bool ShowWindow(nint hWnd)
{
return PInvoke.ShowWindow(new HWND(hWnd), SHOW_WINDOW_CMD.SW_SHOW);
}

/// <summary>
///
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
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);
}

/// <summary>
///
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
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);
}

/// <summary>
///
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions src/libs/H.NotifyIcon/Interop/User32Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit d1ec437

Please sign in to comment.