简体   繁体   中英

Minimize entire WPF application to System Tray in C#

In my wpf application, I've 4 View windows. I've written following code, which minimizes my main window to system tray when minimized that window. But, I want this effect for all windows in my application(ie entire wpf application). When I move from one window to another, it should show me icon in system tray for that window as well. Do I need to write same code for all the other windows? Or any other way to achieve this result?

Code written for minimizing Main window to System Tray,

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

            Globals.ni = new NotifyIcon();
            Globals.ni.Icon = new System.Drawing.Icon(@"D:\TimeSheetIcon.ico");
            Globals.ni.Visible = true;
            Globals.ni.Click +=
                delegate(object sender, EventArgs args)
                {
                    this.Show();
                    this.WindowState = WindowState.Normal;
                };

    }

    protected override void OnStateChanged(EventArgs e)
    {
        if (WindowState == System.Windows.WindowState.Minimized)
        {
            this.Hide();
            Globals.ni.BalloonTipTitle = "MonthView";
            Globals.ni.BalloonTipText = "This is main window";
            Globals.ni.Visible = true;
            Globals.ni.ShowBalloonTip(500);
            base.OnStateChanged(e);
        }
    }
}

NotifyIcon declared globally in Globals.cs

public static class Globals
{
    public static NotifyIcon ni;
}

You should Create a Class (say WindowBase) that Inherits Window class and then apply this above functionality in this new WindowBase class and then Inherit all your new Windows from this WindowBase class. I hope this will give you an idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM