Added Vanara.Windows.Taskbar class to get information on the Shell's taskbar.

pull/285/head
dahall 2022-03-14 09:25:40 -06:00
parent 9b841bdd15
commit d9259ee182
2 changed files with 123 additions and 4 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
@ -7,7 +8,32 @@ namespace Vanara.Windows.Shell
{
internal static class TaskAgg
{
public static Task CompletedTask => Task.CompletedTask;
#if !(NET46_OR_GREATER || NETSTANDARD1_2_OR_GREATER || NETCOREAPP)
private static Task _completedTask;
#endif
public static Task CompletedTask
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
#if NET46_OR_GREATER || NETSTANDARD1_2_OR_GREATER || NETCOREAPP
return Task.CompletedTask;
#else
return _completedTask ??= FromResult(false);
#endif
}
}
public static Task<TResult> FromResult<TResult>(TResult result)
{
#if NET45_OR_GREATER || NETSTANDARD || NETCOREAPP
return Task.FromResult<TResult>(result);
#else
var completionSource = new TaskCompletionSource<TResult>();
completionSource.TrySetResult(result);
return completionSource.Task;
#endif
}
public static Task Run(Action action, CancellationToken cancellationToken)
{

View File

@ -1,18 +1,27 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Vanara.Extensions;
using Vanara.InteropServices;
using Vanara.PInvoke;
using static Vanara.PInvoke.ComCtl32;
using static Vanara.PInvoke.Shell32;
using static Vanara.PInvoke.User32;
namespace Vanara.Windows.Shell.TaskBar
{
/// <summary>Provides information about and some control of the system taskbar.</summary>
public static class Taskbar
{
private const string NotifyWndClass = "TrayNotifyWnd";
private const string PagerWndClass = "SysPager";
private const string TaskbarWndClass = "Shell_TrayWnd";
private const string ToolbarWndClass = "ToolbarWindow32";
private static readonly Lazy<bool> isMinWin7 = new(() => PInvokeClientExtensions.IsPlatformSupported(PInvokeClient.Windows7));
private static bool autoHide, alwaysTop;
private static ABE edge;
private static HWND hwnd;
private static readonly Lazy<bool> isMinWin7 = new(() => PInvokeClientExtensions.IsPlatformSupported(PInvokeClient.Windows7));
private static RECT rect;
/// <summary>
@ -69,13 +78,47 @@ namespace Vanara.Windows.Shell.TaskBar
get { GetPos(); return hwnd; }
}
/// <summary>Gets the tray icons.</summary>
/// <value>The tray icons.</value>
public static IEnumerable<TrayIcon> TrayIcons
{
get
{
const TBIF bif = TBIF.TBIF_BYINDEX | TBIF.TBIF_COMMAND | TBIF.TBIF_SIZE | TBIF.TBIF_STATE | TBIF.TBIF_STYLE | TBIF.TBIF_TEXT;
var hwnd = GetTrayWindow();
if (hwnd.IsNull) yield break;
using SafeCoTaskMemString strBuf = new(512);
TBBUTTONINFO defBI = new() { cbSize = (uint)Marshal.SizeOf(typeof(TBBUTTONINFO)), dwMask = bif, pszText = strBuf, cchText = strBuf.Capacity };
var cnt = SendMessage(hwnd, ToolbarMessage.TB_BUTTONCOUNT).ToInt32();
var badRes = new IntPtr(-1);
for (int i = 0; i < cnt; i++)
{
TBBUTTONINFO btnInf = defBI;
btnInf.cchText = strBuf.Capacity;
strBuf[0] = '\0';
Win32Error.ThrowLastErrorIf(SendMessage(hwnd, ToolbarMessage.TB_GETBUTTONINFOW, (IntPtr)i, ref btnInf), p => p == badRes);
yield return new(btnInf);
}
}
}
/// <summary>Broadcasts the "TaskbarCreated" message to all windows. This is generally used to refresh the taskbar icons.</summary>
public static void BroadcastTaskbarCreated()
{
var msg = Win32Error.ThrowLastErrorIf(RegisterWindowMessage("TaskbarCreated"), i => i == 0);
Win32Error.ThrowLastErrorIfFalse(SendNotifyMessage(HWND.HWND_BROADCAST, msg));
}
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;
hwnd = abd.hWnd == default ? User32.FindWindowEx(default, default, TaskbarWndClass, null) : abd.hWnd;
}
private static void GetState()
@ -86,6 +129,24 @@ namespace Vanara.Windows.Shell.TaskBar
alwaysTop = isMinWin7.Value || state.IsFlagSet(ABS.ABS_ALWAYSONTOP);
}
private static HWND GetTrayWindow()
{
HWND hwnd = FindWindow(TaskbarWndClass, "");
if (!hwnd.IsNull)
{
hwnd = FindWindowEx(hwnd, default, NotifyWndClass, "");
if (!hwnd.IsNull)
{
hwnd = FindWindowEx(hwnd, default, PagerWndClass, "");
if (!hwnd.IsNull)
{
return FindWindowEx(hwnd, default, ToolbarWndClass, null);
}
}
}
return hwnd;
}
private static void SetPos()
{
APPBARDATA abd = new(hwnd, 0, edge, rect);
@ -103,4 +164,36 @@ namespace Vanara.Windows.Shell.TaskBar
SHAppBarMessage(ABM.ABM_SETSTATE, ref abd);
}
}
/// <summary></summary>
public class TrayIcon
{
internal TrayIcon(in TBBUTTONINFO inf)
{
CommandId = inf.idCommand;
ImageIdx = inf.iImage;
State = inf.fsState;
Style = inf.fsStyle;
Width = inf.cx;
Text = StringHelper.GetString(inf.pszText, CharSet.Unicode, inf.cchText);
}
/// <summary>Command identifier of the button.</summary>
public int CommandId { get; }
/// <summary>Image index of the button.</summary>
public int ImageIdx { get; }
/// <summary>State flags of the button. This can be one or more of the values listed in Toolbar Button States.</summary>
public TBSTATE State { get; }
/// <summary>Style flags of the button. This can be one or more of the values listed in Toolbar Control and Button Styles.</summary>
public ToolbarStyle Style { get; }
/// <summary>Address of a character buffer that contains or receives the button text.</summary>
public string Text { get; }
/// <summary>Width of the button, in pixels.</summary>
public ushort Width { get; }
}
}