Can I use NotifyIcon in WPF?
Asked Answered
F

3

21

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

 }
Fluviatile answered 16/7, 2013 at 10:59 Comment(1)
I think this is a duplicate #10231079Vitek
E
28

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

Epicritic answered 16/7, 2013 at 11:4 Comment(1)
Thanks. After adding System.Windows.Forms and System.Drawing(for Icon) in the references of the application, it works fine.Fluviatile
B
13

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.

EDIT:

There is a realy easy but powerfull way to show toast messages via Windows Notification Center. Sinds Windows 10, this is the most common way to show notifications and not via ShowBalloonTip anymore.

More information can be found here.

Bigwig answered 3/6, 2016 at 13:58 Comment(0)
C
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.

Chelton answered 16/7, 2013 at 11:5 Comment(7)
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?Fluviatile
@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.Chelton
For creating NotifyIcon, which namespace should I use?Fluviatile
System.Windows.Forms...It is all outlined and explained in the first link in my answer. Also checkout msdn.microsoft.com/en-us/library/… for deeper knowledge about NotifyIconEpicritic
@Chelton 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?Mackler
@AllanRuin: what kind of error are you talking about ?Chelton
@Chelton 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..Mackler

© 2022 - 2025 — McMap. All rights reserved.