Windows Shell NotifyIcon Library
Confirmed to be usable from WinUI 3 applications.
using YDKK.Windows;
var icon = Icon.FromFile("path-to-icon.ico");
// You can specify null for second argument if the icon is not needed or for testing.
var notifyIcon = new NotifyIcon("Tooltip-Text", icon);
notifyIcon.LButtonDoubleClick += (args) =>
{
Console.WriteLine("NotifyIcon double clicked!");
Console.WriteLine($"Mouse cursor position: ({args.xPos}, {args.yPos})");
};
At this time, the Win32 API is required to achieve this.
// Prepare popup menu
var exitCommandId = 0;
var exitLabel = "Exit";
var menu = PInvoke.CreatePopupMenu();
var info = new MENUITEMINFOW
{
cbSize = (uint)Marshal.SizeOf<MENUITEMINFOW>(),
fMask = MENU_ITEM_MASK.MIIM_ID | MENU_ITEM_MASK.MIIM_STRING,
wID = exitCommandId,
cch = (uint)(exitLabel.Length * sizeof(char)),
};
fixed (char* ptr = exitLabel)
{
info.dwTypeData = ptr;
}
PInvoke.InsertMenuItem(menu, 0, true, &info);
// Show context menu
notifyIcon.RButtonUp += (args) =>
{
var hWnd = (HWND)notifyIcon.WindowHandle;
PInvoke.SetForegroundWindow(hWnd);
PInvoke.TrackPopupMenuEx(menu, (uint)TRACK_POPUP_MENU_FLAGS.TPM_LEFTALIGN, args.xPos, args.yPos, hWnd);
};
// Receive results
NotifyIcon.MenuCommand += (args) =>
{
switch (args)
{
case exitCommandId:
Console.WriteLine("Exit menu command selected.")
break;
}
};
At this time, supported build platform is x64
only.
MIT