tags:

views:

1611

answers:

4

I am a total WPF newbie and wonder if anyone could give me some pointers how to write an application that starts minimized to tray. The idea is that it periodically fetches an RSS Feed and creates a Toaster-Popup when there are new feeds.

The Application should still have a Main Window (essentially just a list containing all feed entries), but that should be hidden by default.

I have started reading about XAML and WPF and I know that the StartupUri in the App.xaml has to point to my main window, but I have no idea what the proper way is to do the SysTray icon and hide the main window (this also means that when the user minimizes the window, it should minimize to tray, not to taskbar).

Any hints?

+1  A: 

Here you go.

luvieere
+8  A: 

There's no NotifyIcon for WPF (yet).

A colleague of mine used this freely available library to good effect:

http://www.hardcodet.net/projects/wpf-notifyicon

Drew Noakes
+11  A: 

I recently had this same problem. Unfortunately, NotifyIcon is only a Windows.Forms control at the moment, if you want to use it you are going to have to include that part of the framework. I guess that depends how much of a WPF purist you are.

If you want a quick and easy way of getting started check out this WPF NotifyIcon control on the Code Project which does not rely on the WinForms NotifyIcon at all. This seems like the best and cleanest way to me so far.

  • Rich ToolTips rather than text
  • WPF context menus and popups
  • Command support and routed events
  • Flexible data binding
  • Rich balloon messages rather than the default messages provides by the OS

Check it out. It comes with an amazing sample app too, very easy to use, and you can have great looking Windows Live Messenger style WPF popups, tooltips, and context menus. Perfect for displaying an RSS feed, I am using it for a similar purpose.

Dale Halliwell
That codeproject article is the same one I linked to in my answer, only the version on codeproject is out of date.
Drew Noakes
+3  A: 

You have to use the NotifyIcon control from System.Windows.Forms, or alternatively you can use the Notify Icon API provided by Windows API. WPF Provides no such equivalent, and it has been requested on Microsoft Connect several times.

I have active, working code which uses System.Windows.Forms NotifyIcon Component from a WPF application, the code can be viewed at http://mubox.codeplex.com/SourceControl/changeset/view/30984#229523

Here are the summary bits:

Create a WPF Window with ShowInTaskbar=False, and which is loaded in a non-Visible State.

At class-level:

    private System.Windows.Forms.NotifyIcon notifyIcon = null;

During OnInitialize():

        notifyIcon = new System.Windows.Forms.NotifyIcon();
        notifyIcon.Click += new EventHandler(notifyIcon_Click);
        notifyIcon.DoubleClick += new EventHandler(notifyIcon_DoubleClick);
        notifyIcon.Icon = IconHandles["QuickLaunch"];

During OnLoaded():

        notifyIcon.Visible = true;

And for interaction (shown as notifyIcon.Click and DoubleClick above):

    void notifyIcon_Click(object sender, EventArgs e)
    {
        ShowQuickLaunchMenu();
    }

From here you can resume the use of WPF Controls and APIs such as context menus, pop-up windows, etc.

It's that simple. You don't exactly need a WPF Window to host to the component, it's just the most convenient way to introduce one into a WPF App (as a Window is generally the default entry point defined via App.xaml), likewise, you don't need a WPF Wrapper or 3rd party control, as the SWF component is guaranteed present in any .NET Framework installation which also has WPF support (3.0, 3.5, eventually 4.0) since it's part of the 2.0 Framework (which all current and future Frameworks are built upon.)

Hope that helps.

It's a little cheese that you have to use a pre-3.0 Framework Component to get a tray-icon, but understandably as Microsoft has explained it, there is no concept of a System Tray within the scope of WPF. WPF is a presentation technology, and Notification Icons are an Operating System (not a "Presentation") concept.

Shaun Wilson
Thanks for the detailed Explanation!
Michael Stum