A WPF wrapper around Windows Form NotifyIcon

Because WPF do not have a Notify Icon control, I decided implement a WPF control that wraps the Windows Form control. It only covers the basic functionality, but I believe that it is enough for most scenarios. Enjoy it !

The control and the sample are here.

17 Comments

  • Hi, im using the following code to create this:



    But it tells me the vlaue is outside of the expected range. Whats is causing this?

    Thanks

  • The icon needs to be defined as a enbedded resource inside the project.

  • When I try use the example in VS2008 or Expression Blend 2, I get the following error:

    Unsupported URI syntax.
    Method expects a relative URI or a pack://application:.../ form of absolute URL

  • I do not know why this happen. Maiby it is a bug in both designers.

  • Hi marianor, thanks for the very handy wrapper. I am having a bit of trouble with the ContextMenu that shows up on a right-click. The problem is it is not responsive to going-away at the proper times. For example, hitting the escape key does nothing, and opening another menu (for another tray icon, e.g.) is the same.

    Any thoughts on how to make this menu disappear properly?

  • To stop this error, define the icon in code and set the icon to the notifyIcon.Icon property.

    Uri iconUri = new Uri("pack://application:,,,/Resources/USERCARD_2.ico", UriKind.RelativeOrAbsolute);
    notifyIcon.Icon = BitmapFrame.Create(iconUri);

  • Been looking for a solution to this problem for awhile now. Thanks for sharing and seasons greetings :)

  • You dont happen to have this for VB or must i convert it =)

  • Sorry, I do not have this code for VB.NET.

  • The wrapper is fantastic, thank you!

    Has anyone found a solution to the context menu staying open? I can't find any reliable way to make the context menu behave like it should.

  • An easy way to solve the ContextMenu staying open is adding an event MouseLeave in Xaml file and event handler in .xaml.cs file as follows.
    In .xaml file:

    In .xaml.cs file:
    private void OnMouseLeave(object sender,
    RoutedEventArgs e)
    {
    this.NotifyIcon.ContextMenu.IsOpen = false;
    }

    It works for me.

    David Z.

  • I did some changes to support the closing of the context menu when you click in other application. This changes involves API invokes, and all the code is implemented in a partial class in order you can remove this class if you ar not interested in that functionallity.

  • Thanks for this but....
    I'm using this in my own project and I'm getting the error
    "The file Resources/NotifyIcon.ico is not part of the project or its 'Build Action' property is not set to 'Resource'."
    I don't understand why. Everything looks ok.

  • You need to include the NotifyIcon.ico file in a folder colled Resources, apart from that you have to set the build action of this file as "Resource".

  • marianor. Your fix for the context menu's not going away should be modified. Your call to SetWindowsHookEx is passing in a non-static method with no delegate reference that stays around in memory. Without this the reference is getting garbage collected. And cheers for the code!

  • Thanks for reporting this, it is a good point the component was fixed.

  • Hi, excellent component! But I am having problems with binding contextmenu like this:



    It only shows a tiny box near the icon. If I create a contextmenu for the StackPanel it works fine for any element on the sample form.
    myContextMenu is ObservableCollection.
    Any ideas?
    Thanks

Comments have been disabled for this content.