Nicolai
Nicolai

Reputation: 21

Shell_NotifyIcon in WinUI 3

I'm trying to build an WinUI 3 Application with an Notify Icon (Systray Icon). Therefore I'm using the Win32-Api: https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shell_notifyicona

So far I got the icon to work, but know I'm trying to get a context menu on it. I found examples for it, but there are all using Windows Forms (like the Github Project EarTrumpet). I can't find a solution to handle the Callback. (something like a WndProc-Method)

Here is my code so far:

public void NotifyIcon(IntPtr parent)
    {
        
        var _data = new NotifyIconData();
        _data.cbSize = Marshal.SizeOf(typeof(NotifyIconData));
        _data.uID = 1;
        _data.uFlags = 0x8 | 0x2 | 0x1; //NIF_STATE | NIF_ICON | NIF_MESSAGE
        _data.dwState = 0x0;
        _data.hIcon = SystemIcons.Information.Handle;
        _data.hWnd = parent;
        _data.uCallbackMessage = 0x5700;
        Shell_NotifyIcon(0x0, ref _data);
    }
struct NotifyIconData
    {
        public System.Int32 cbSize;
        public System.IntPtr hWnd; // HWND
        public System.Int32 uID; // UINT
        public System.Int32 uFlags; // UINT
        public System.Int32 uCallbackMessage;  // UINT
        public System.IntPtr hIcon;  // HICON
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public System.String szTip;   // char[128]
        public System.Int32 dwState;  // DWORD
        public System.Int32 dwStateMask;  // DWORD
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
        public System.String szInfo; // char[256]
        public System.Int32 uTimeoutOrVersion;  // UINT
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
        public System.String szInfoTitle; // char[64]
        public System.Int32 dwInfoFlags;  // DWORD
        public Guid guidItem;
        public IntPtr hBalloonIcon; //HIcon
    }

Im trying to handle the Callback in the parent window.

If you need more information I'm happy to provide it. This is my second post here, so I'm quite new :)

Upvotes: 2

Views: 2138

Answers (1)

Konstantin S.
Konstantin S.

Reputation: 1505

I know of two implementations of this in WinUI 3 in the form of ready-made NuGet libraries:

Upvotes: 2

Related Questions