2

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 :)

Nicolai
  • 21
  • 3
  • You can [get the `HWND` off of a `CoreWindow`](https://stackoverflow.com/q/34935077/1889329). I don't know whether that gets you closer to a solution. I also don't know how much of the raw message dispatching is available to clients using WinUI3 or the [Windows App SDK](https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/). – IInspectable Nov 12 '21 at 19:09

1 Answers1

0

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

Konstantin S.
  • 1,307
  • 14
  • 19
  • 1
    Thank you, that already helps alot! You also created a WindowLess Example-App. Is this also possibly in WinUI? I Haven't found an equivalent to ShutDownMode :) – Nicolai Mar 30 '22 at 14:25
  • 1
    I have added simple context menu support and WinUI Windowless example app: https://github.com/HavenDV/H.NotifyIcon/tree/master/src/apps/H.NotifyIcon.Apps.WinUI.Windowless – Konstantin S. Apr 02 '22 at 10:40