H.NotifyIcon

repository·master·Indexed 20 days ago

https://github.com/havendv/h.notifyicon

A cross-platform NotifyIcon (system tray icon) implementation for .NET 6+ targeting WPF, WinUI, Uno Platform, and Console applications. It provides features such as custom tooltips, popups, context menus, and GeneratedIconSource for dynamic icons without relying on Windows Forms. Includes support for Windows 11 Efficiency Mode and customizable WinUI context menu themes.

Tokens
1.7K
Snippets
5
Records
5
Agent score
23%

What's inside H.NotifyIcon

  1. Install H.NotifyIcon via NuGet

    master

    Install the appropriate package for your target platform using NuGet. Use the core H.NotifyIcon package if you are building a console application or need a platform-agnostic core library.

    # For WPF
    Install-Package H.NotifyIcon.Wpf
    
    # For WinUI
    Install-Package H.NotifyIcon.WinUI
    
    # For Uno Platform
    Install-Package H.NotifyIcon.Uno
    
    # For Uno.WinUI
    Install-Package H.NotifyIcon.Uno.WinUI
    
    # For Core/Console applications
    Install-Package H.NotifyIcon
  2. Configure WinUI Context Menus and Themes

    master

    WinUI users can customize how context menus are rendered and themed.

    Modes:

    • PopupMenu (Default): Creates a Win32 PopupMenu based on your MenuFlyout.
    • SecondWindow: Creates a second transparent window to render the native menu (currently in preview). Set ContextMenuMode="SecondWindow".

    Theming: Use ContextMenuThemeMode to control the visual style:

    • System: Follows the Windows app theme (default).
    • Light: Forces a light theme.
    • Dark: Forces a dark theme.
    • Default: Uses the Windows default native menu app mode.

    Direct H.NotifyIcon.Core.PopupMenu users can use the ThemeMode property.

    <tb:TaskbarIcon
        ContextMenuMode="PopupMenu"
        ContextMenuThemeMode="Dark"
        />
  3. Generate dynamic icons with GeneratedIconSource

    master

    Instead of using a static .ico file, you can use GeneratedIconSource to create icons from text, emojis, or complex visual styles (including gradients and borders) directly in XAML.

    <!-- Simple Text/Emoji Icon -->
    <tb:TaskbarIcon>
        <tb:TaskbarIcon.IconSource>
            <tb:GeneratedIconSource
                Text="❤️"
                Foreground="Red"
                />
        </tb:TaskbarIcon.IconSource>
    </tb:TaskbarIcon>
    
    <!-- Icon with specific Font settings -->
    <tb:TaskbarIcon>
        <tb:TaskbarIcon.IconSource>
            <tb:GeneratedIconSource
                Text="5"
                Foreground="Black"
                FontSize="36"
                FontWeight="Bold"
                />
        </tb:TaskbarIcon.IconSource>
    </tb:TaskbarIcon>
    
    <!-- Complex Icon with Gradients and Borders -->
    <tb:TaskbarIcon>
        <tb:TaskbarIcon.IconSource>
            <tb:GeneratedIconSource
                Text="❤️"
                BorderThickness="5"
                FontSize="46"
                >
                <tb:GeneratedIconSource.Foreground>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="128,128">
                        <GradientStop Color="White" />
                        <GradientStop Color="Red" />
                    </LinearGradientBrush>
                </tb:GeneratedIconSource.Foreground>
                <tb:GeneratedIconSource.BorderBrush>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="128,128">
                        <GradientStop Color="White" />
                        <GradientStop Color="Red" />
                    </LinearGradientBrush>
                </tb:GeneratedIconSource.BorderBrush>
            </tb:GeneratedIconSource>
        </tb:TaskbarIcon.IconSource>
    </tb:TaskbarIcon>
  4. Use TaskbarIcon in XAML

    master

    The TaskbarIcon control can be embedded directly in XAML. You must declare the correct namespace based on your platform (WPF uses clr-namespace:H.NotifyIcon;assembly=H.NotifyIcon.Wpf, while WinUI uses using:H.NotifyIcon).

    <Window
        xmlns:tb="clr-namespace:H.NotifyIcon;assembly=H.NotifyIcon.Wpf" // WPF
        xmlns:tb="using:H.NotifyIcon" // WinUI
        >
        <tb:TaskbarIcon
            ToolTipText="ToolTip"
            IconSource="/Images/TrayIcons/Logo.ico"
            ContextMenu="{StaticResource TrayMenu}"
            MenuActivation="LeftOrRightClick"
            TrayPopup="{StaticResource TrayStatusPopup}"
            PopupActivation="DoubleClick"
            TrayToolTip="{StaticResource TrayToolTip}"
            />
    </Window>
  5. Configure Efficiency Mode for background applications

    master

    For applications intended to run in the background, you can leverage Windows 11's Efficiency Mode using the following API methods:

    • EfficiencyModeUtilities.SetEfficiencyMode(bool value): Directly sets the mode.
    • WindowExtensions.Hide(this Window window, enableEfficiencyMode: true): Hides the window and enables Efficiency Mode (default behavior).
    • WindowExtensions.Show(this Window window, disableEfficiencyMode: true): Shows the window and disables Efficiency Mode.
    • TaskbarIcon.ForceCreate(bool enablesEfficiencyMode = true): Forces creation of the icon with Efficiency Mode settings.
    EfficiencyModeUtilities.SetEfficiencyMode(bool value)
    WindowExtensions.Hide(this Window window, enableEfficiencyMode: true)
    WindowExtensions.Show(this Window window, disableEfficiencyMode: true)
    TaskbarIcon.ForceCreate(bool enablesEfficiencyMode = true)