YDKK.Windows.NotifyIcon 1.0.0

dotnet add package YDKK.Windows.NotifyIcon --version 1.0.0                
NuGet\Install-Package YDKK.Windows.NotifyIcon -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="YDKK.Windows.NotifyIcon" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add YDKK.Windows.NotifyIcon --version 1.0.0                
#r "nuget: YDKK.Windows.NotifyIcon, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install YDKK.Windows.NotifyIcon as a Cake Addin
#addin nuget:?package=YDKK.Windows.NotifyIcon&version=1.0.0

// Install YDKK.Windows.NotifyIcon as a Cake Tool
#tool nuget:?package=YDKK.Windows.NotifyIcon&version=1.0.0                

YDKK.Windows.NotifyIcon

Windows Shell NotifyIcon Library

Confirmed to be usable from WinUI 3 applications.

#

Usage

using YDKK.Windows;

Show NotifyIcon in the task tray and receive mouse events

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})");
};

Show context menu when NotifyIcon is right-clicked and receive results

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(PopupMenu, 0, true, &info);

// Show context menu
notifyIcon.RButtonUp += (args) =>
{
    var hWnd = (HWND)notifyIcon.WindowHandle;
    PInvoke.SetForegroundWindow(hWnd);
    PInvoke.TrackPopupMenuEx(PopupMenu, (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;
    }
};

Note

At this time, supported build platform is x64 only.

License

MIT

Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0-windows7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 253 5/15/2022