Giter Club home page Giter Club logo

ydkk.windows.notifyicon's Introduction

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(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;
    }
};

Note

At this time, supported build platform is x64 only.

License

MIT

ydkk.windows.notifyicon's People

Contributors

ydkk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.