19

I want to minimizing application to system tray using WPF. Is "NotifyIcon" is the only way to achieve this result? If yes, which namespace is required for using "NotifyIcon" in WPF?

If possible with "NotifyIcon",please provide some hint, how can I use that in my Mainwindow?

My main window is,

public partial class MonthView : MetroWindow
{

    public DateTime SelectedDate { get; set; }

    public MonthView()
    {

            InitializeComponent();
            calMain.DisplayDate = DateTime.Today;
            Globals._globalController = new AppController();
            Globals._globalController.appTaskManager.setupLocal();
            Globals._globalController.setMonthViewWindow(this);

    }

    public void calItemSelectedDate(object sender, SelectionChangedEventArgs e)
    {
        DateTime d;
        if (sender is DateTime)
        {
            d = (DateTime)sender;
        }
        else
        {
            DateTime.TryParse(sender.ToString(), out d);
        }

        SelectedDate = d;

        ShowActivity(d);
     }

    public void ShowActivity(DateTime date)
    {
        DayView Activity = new DayView(date);
        Activity.Show();
        this.Hide();
    }

    private void SetButton_Click(object sender, RoutedEventArgs e)
    {
        SettingsView set = new SettingsView();
        set.Show();
        this.Hide();
    }

 }
Dinesh
  • 507
  • 4
  • 14
  • 23
  • I think this is a duplicate http://stackoverflow.com/questions/10230579/easiest-way-to-have-a-program-minimize-itself-to-the-system-tray-using-net-4 – Swift Jul 16 '13 at 11:02

3 Answers3

27

NotifyIcon is not implemented in WPF as it is in Forms, but you can still use the Windows Form NotifyIcon, it resides in the System.Windows.Forms namspace.

Take a look at these tutorials, they might cover your needs:

Simple solution, directly using NotifyIcon: http://www.abhisheksur.com/2012/08/notifyicon-with-wpf-applications.html

More advanced solution, new library based on NotifyIcon with more features: http://www.codeproject.com/Articles/36468/WPF-NotifyIcon

More info about NotifyIcon can be found here: http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx

AndyUK
  • 3,933
  • 7
  • 42
  • 44
Jesper Jensen
  • 585
  • 6
  • 16
  • 2
    Thanks. After adding System.Windows.Forms and System.Drawing(for Icon) in the references of the application, it works fine. – Dinesh Jul 17 '13 at 06:43
12

You can set your code for the NotifyIcon in App.xaml.cs

using System.Drawing;

namespace DDD
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        System.Windows.Forms.NotifyIcon nIcon = new System.Windows.Forms.NotifyIcon();
        public App()
        {
            nIcon.Icon = new Icon(@"path to ico");
            nIcon.Visible = true;
            nIcon.ShowBalloonTip(5000, "Title", "Text",  System.Windows.Forms.ToolTipIcon.Info);
            nIcon.Click += nIcon_Click;
        }

        void nIcon_Click(object sender, EventArgs e)
        {
            //events comes here
            MainWindow.Visibility = Visibility.Visible;
            MainWindow.WindowState = WindowState.Normal;
        }
    }
}

And in your Mainwindow.xaml.cs:

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        e.Cancel = true;
        this.Visibility = Visibility.Hidden;
    }

Make sure that Window_Closing is binded to the closing event of the main window.

If you "close" your main window, the window visibility wil be set to hidden but your app will be still running. Simple click on the NotifyIcon in the notification area and there is your window back.

Stef Geysels
  • 1,023
  • 11
  • 27
6

Yes, it is possible, and I used it with success in my personal project. There is an excelent control written by Philip Sumi http://www.hardcodet.net/projects/wpf-notifyicon. I used precisly that one and it works really great and looks nice (subjective).

image

Just note: pay attention licensing terms, check out if you can use it in your project.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • I've seen the link which you mentioned in the description. But, I don't understand, how could I use that in my View window? – Dinesh Jul 16 '13 at 11:12
  • @Dinesh: you need to use it like a WPF Control. It's not something that can be explained in short answer here. Download code, run a sample and see how it's used. – Tigran Jul 16 '13 at 11:14
  • For creating NotifyIcon, which namespace should I use? – Dinesh Jul 16 '13 at 12:00
  • System.Windows.Forms...It is all outlined and explained in the first link in my answer. Also checkout http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx for deeper knowledge about NotifyIcon – Jesper Jensen Jul 17 '13 at 06:00
  • @Tigran I try to use WPF NotifyIcon's windowless example in my project, but the notify-icon does not showup.. and I encounter no error. any idea what may go wrong? – Allan Ruin Jul 26 '17 at 10:51
  • @AllanRuin: what kind of error are you talking about ? – Tigran Jul 26 '17 at 13:08
  • 1
    @Tigran Orz, it's a silly problem.. I had scrap my head for a afternoon.. I follow the author's windowless example. But as a newbie in WPF, I do not realize there is a APP.xaml.cs file associate with APP.xaml where the sample code have some lines in it... Now it all works.. – Allan Ruin Jul 26 '17 at 13:15