diff --git a/UnitTests/Windows.Shell/TaskbarTests.cs b/UnitTests/Windows.Shell/TaskbarTests.cs new file mode 100644 index 00000000..e85817a1 --- /dev/null +++ b/UnitTests/Windows.Shell/TaskbarTests.cs @@ -0,0 +1,20 @@ +using NUnit.Framework; +using Vanara.PInvoke; +using Vanara.PInvoke.Tests; + +namespace Vanara.Windows.Shell.Tests +{ + [TestFixture] + public class TaskbarTests + { + [Test] + public void TaskbarTest() + { + Assert.That(TaskBar.Taskbar.Handle, ResultIs.ValidHandle); + Assert.That(TaskBar.Taskbar.Bounds, Is.Not.EqualTo(RECT.Empty)); + Assert.That(() => TaskBar.Taskbar.AutoHide, Throws.Nothing); + + TestContext.Write($"{TaskBar.Taskbar.Bounds}; {TaskBar.Taskbar.AutoHide}; {TaskBar.Taskbar.Edge}"); + } + } +} \ No newline at end of file diff --git a/Windows.Shell.Common/TaskBar/Taskbar.cs b/Windows.Shell.Common/TaskBar/Taskbar.cs new file mode 100644 index 00000000..8f83ad2a --- /dev/null +++ b/Windows.Shell.Common/TaskBar/Taskbar.cs @@ -0,0 +1,106 @@ +using System; +using Vanara.Extensions; +using Vanara.PInvoke; + +using static Vanara.PInvoke.Shell32; + +namespace Vanara.Windows.Shell.TaskBar +{ + /// Provides information about and some control of the system taskbar. + public static class Taskbar + { + private static bool autoHide, alwaysTop; + private static ABE edge; + private static HWND hwnd; + private static readonly Lazy isMinWin7 = new(() => PInvokeClientExtensions.IsPlatformSupported(PInvokeClient.Windows7)); + private static RECT rect; + + /// + /// Gets or sets a value indicating whether the taskbar is always on top. This value is always true for systems since Windows 7. + /// + /// if always on top; otherwise, . + public static bool AlwaysOnTop + { + get { GetState(); return alwaysTop; } + set + { + if (!isMinWin7.Value) + { + GetState(); + if (alwaysTop != value) + { + alwaysTop = value; + SetState(); + } + } + else + throw new PlatformNotSupportedException(); + } + } + + /// Gets or sets a value indicating whether the taskbar is configured to automatically hide itself when not used. + /// if automatically hidden; otherwise, . + public static bool AutoHide + { + get { GetState(); return autoHide; } + set { GetState(); if (autoHide != value) { autoHide = value; SetState(); } } + } + + /// Gets the bounds of the taskbar. + /// The bounds. + public static RECT Bounds + { + get { GetPos(); return rect; } + //set { GetPos(); if (rect != value) { rect = value; SetPos(); } } + } + + /// Gets a value that determines on which edge of the screen the taskbar is displayed. + /// The screen edge. + public static ABE Edge + { + get { GetPos(); return edge; } + //set { GetPos(); if (edge != value) { edge = value; SetPos(); } } + } + + /// Gets the window handle of the System Taskbar. + /// The window handle. + public static HWND Handle + { + get { GetPos(); return hwnd; } + } + + private static void GetPos() + { + APPBARDATA abd = new(default); + Win32Error.ThrowLastErrorIf(SHAppBarMessage(ABM.ABM_GETTASKBARPOS, ref abd), r => r == IntPtr.Zero); + rect = abd.rc; + edge = abd.uEdge; + hwnd = abd.hWnd == default ? User32.FindWindowEx(default, default, "Shell_TrayWnd", null) : abd.hWnd; + } + + private static void GetState() + { + APPBARDATA abd = new(default); + ABS state = (ABS)SHAppBarMessage(ABM.ABM_GETSTATE, ref abd).ToInt32(); + autoHide = state.IsFlagSet(ABS.ABS_AUTOHIDE); + alwaysTop = isMinWin7.Value || state.IsFlagSet(ABS.ABS_ALWAYSONTOP); + } + + private static void SetPos() + { + APPBARDATA abd = new(hwnd, 0, edge, rect); + SHAppBarMessage(ABM.ABM_SETPOS, ref abd); + } + + private static void SetState() + { + if (hwnd == default) + { + GetPos(); + } + + APPBARDATA abd = new(hwnd, lParam: (int)((autoHide ? ABS.ABS_AUTOHIDE : 0) | (alwaysTop ? ABS.ABS_ALWAYSONTOP : 0))); + SHAppBarMessage(ABM.ABM_SETSTATE, ref abd); + } + } +} \ No newline at end of file