Scarlet.Extensions.Hosting.DirectN.TrayIcon 1.1.0

Prefix Reserved
dotnet add package Scarlet.Extensions.Hosting.DirectN.TrayIcon --version 1.1.0
                    
NuGet\Install-Package Scarlet.Extensions.Hosting.DirectN.TrayIcon -Version 1.1.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="Scarlet.Extensions.Hosting.DirectN.TrayIcon" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Scarlet.Extensions.Hosting.DirectN.TrayIcon" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Scarlet.Extensions.Hosting.DirectN.TrayIcon" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Scarlet.Extensions.Hosting.DirectN.TrayIcon --version 1.1.0
                    
#r "nuget: Scarlet.Extensions.Hosting.DirectN.TrayIcon, 1.1.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.
#:package Scarlet.Extensions.Hosting.DirectN.TrayIcon@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Scarlet.Extensions.Hosting.DirectN.TrayIcon&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Scarlet.Extensions.Hosting.DirectN.TrayIcon&version=1.1.0
                    
Install as a Cake Tool

Scarlet.Extensions.Hosting.DirectN.TrayIcon

A notification area (tray) icon for Scarlet.Extensions.Hosting.DirectN, Native AOT compatible.

TrayNotifyIcon ports the behavior of WinForms' NotifyIcon: the same mouse events in the same order, balloon tips, and re-adding the icon when Explorer restarts. The context menu is a native Win32 popup menu (system style, mnemonics and keyboard access), rebuilt from a TrayMenu each time it opens. Types are prefixed Tray so they don't clash with DirectN.Extensions.Utilities.NotifyIcon.

builder.Services.AddDirectN(o => o.ShutdownMode = ShutdownMode.OnExplicitShutdown);
builder.Services.AddDirectNTrayIcon<TrayIcon>();

public sealed class TrayIcon(IDirectNThread thread) : ITrayIcon
{
    private TrayNotifyIcon? _notifyIcon;
    private TrayIconImage? _image;

    public void InitializeComponent()   // on the UI thread, before the message loop
    {
        _image = TrayIconImage.FromApplicationIcon();   // <ApplicationIcon> of the exe
        _notifyIcon = new TrayNotifyIcon
        {
            Icon = _image,
            Text = $"My app {ApplicationInfo.ProductVersion}",
            ShowContextMenuOnLeftClick = true,
            ContextMenu = new TrayMenu
            {
                Items =
                {
                    new TrayMenuItem(ApplicationInfo.ProductVersion) { Enabled = false },
                    TrayMenuItem.Separator(),
                    new TrayMenuItem("E&xit", (_, _) => thread.HandleApplicationExit()),
                },
            },
            Visible = true,
        };
    }

    public void Dispose()               // on the UI thread, after the message loop
    {
        _notifyIcon?.Dispose();
        _image?.Dispose();
    }
}

API

Type
ITrayIcon An IDirectNComponent + IDisposable; register with AddDirectNTrayIcon<T>() (or a factory).
TrayNotifyIcon Icon, Text (max 127 chars), Visible, ContextMenu, ShowContextMenuOnLeftClick, ShowContextMenu(), ShowBalloonTip(title, text, icon); events Click, DoubleClick, MouseClick, MouseDoubleClick, MouseDown, MouseUp, MouseMove, BalloonTipShown, BalloonTipClicked, BalloonTipClosed. UI thread only (except Dispose).
TrayMenu Items, Opening event (update items right before the menu shows).
TrayMenuItem Text (& mnemonics), Enabled, Checked, IsDefault (bold), Items (submenu), Tag, Click; TrayMenuItem.Separator().
TrayIconImage FromApplicationIcon(), FromResource(id), FromFile(path), FromStream(ico), FromHandle(hicon, ownsHandle). Loaded at the notification area's size for the current DPI. Not owned by the icon: dispose it after the icon.
ApplicationInfo ProductVersion, ProductName: AOT-safe replacements for WinForms' Application.ProductVersion / ProductName.

Differences from WinForms

  • ShowContextMenu() is public: no reflection needed to open the menu on left click (and ShowContextMenuOnLeftClick does it for you).
  • Uses NOTIFYICON_VERSION_4: the menu also opens with Shift+F10 / the context menu key, Enter/Space on the focused icon raise Click, and positions are correct on every monitor.
  • A chosen menu item's Click runs after the menu has closed.
  • With a right-click, the menu opens on WM_CONTEXTMENU, after MouseUp, Click and MouseClick (WinForms opens it on the button release, before them).
  • Dispose can be called from any thread: the owning thread removes the icon the next time it processes messages.
  • Exceptions thrown by event handlers go to IDirectNThread.UnhandledException like any other UI-thread exception.
Product Compatible and additional computed target framework versions.
.NET net10.0-windows7.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.1.0 92 11/5/2026
1.0.0 95 11/3/2026