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.

Published Monday, October 15, 2007 8:09 PM by marianor
Filed under:

Comments

# re: A WPF wrapper around Windows Form NotifyIcon

Thursday, April 03, 2008 9:38 PM by tim

beautiful!

# re: A WPF wrapper around Windows Form NotifyIcon

Sunday, April 06, 2008 3:16 PM by Mike

Hi, im using the following code to create this:

<tn:NotifyIcon x:Name="notifyIcon" Text="Icon" Icon="icon.ico"/>

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

Thanks

# re: A WPF wrapper around Windows Form NotifyIcon

Thursday, April 17, 2008 3:42 PM by marianor

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

# re: A WPF wrapper around Windows Form NotifyIcon

Sunday, June 29, 2008 10:03 AM by Bryan Brannon

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

# re: A WPF wrapper around Windows Form NotifyIcon

Friday, August 15, 2008 3:34 PM by marianor

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

# re: A WPF wrapper around Windows Form NotifyIcon

Tuesday, September 02, 2008 9:19 PM by Logan

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?

# re: A WPF wrapper around Windows Form NotifyIcon

Thursday, September 18, 2008 11:42 AM by Matt

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

# re: A WPF wrapper around Windows Form NotifyIcon

Thursday, December 25, 2008 10:13 AM by Chris

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

# re: A WPF wrapper around Windows Form NotifyIcon

Friday, February 27, 2009 12:26 PM by Alcstudio

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

# re: A WPF wrapper around Windows Form NotifyIcon

Friday, March 06, 2009 6:31 PM by marianor

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

# re: A WPF wrapper around Windows Form NotifyIcon

Thursday, March 26, 2009 5:06 PM by brucy

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.

# re: A WPF wrapper around Windows Form NotifyIcon

Friday, April 10, 2009 3:12 PM by David Z.

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:

<MenuItem Header="Exit" Click="OnExitClick" MouseLeave="OnMouseLeave" />

In .xaml.cs file:

private void OnMouseLeave(object sender,

                         RoutedEventArgs e)

{

  this.NotifyIcon.ContextMenu.IsOpen = false;

}

It works for me.

David Z.

# re: A WPF wrapper around Windows Form NotifyIcon

Thursday, April 30, 2009 7:24 PM by marianor

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.

# re: A WPF wrapper around Windows Form NotifyIcon

Thursday, July 09, 2009 6:50 AM by empo

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.

# re: A WPF wrapper around Windows Form NotifyIcon

Thursday, July 09, 2009 8:35 AM by marianor

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".

# re: A WPF wrapper around Windows Form NotifyIcon

Thursday, July 30, 2009 4:08 PM by Mike Stacie

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!

# re: A WPF wrapper around Windows Form NotifyIcon

Thursday, July 30, 2009 8:49 PM by marianor

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

Leave a Comment

(required) 
(required) 
(optional)
(required)