Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a window from System Tray icon

So I created a window with a system tray icon. The window starts out minimized and will re-appear when the system tray Icon is clicked. However, it ONLY works when you click on the minimize button. If you click the red exit button the window disappears, the System Tray Icon remains(as it should) but when you click on it the program throws an error.

Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed.

Here is the relevant code

public partial class MainWindow : Window
{
    public static NotifyIcon icon;

    List<string> food = new List<string>();
    bool on = false;

    public MainWindow()
    {
        InitializeComponent();

        food.Add("Breakfast");
        food.Add("Soups");
        food.Add("Vegetables");
        food.Add("Crab roll");
        food.Add("Sushi");
        food.Add("Egg rolls");
        food.Add("Salad");


        MainWindow.icon = new NotifyIcon();

        window1.WindowState = WindowState.Minimized; 

        icon.Icon = new System.Drawing.Icon("favicon.ico");
        icon.Visible = true;

        icon.Click += new EventHandler(icon_Click);
        icon.BalloonTipClicked += new EventHandler(icon_BalloonTipClicked);
        icon.DoubleClick += new EventHandler(icon_DoubleClick);
        icon.BalloonTipClosed += new EventHandler(icon_BalloonTipClosed);
        icon.MouseMove += new System.Windows.Forms.MouseEventHandler(icon_MouseMove);
        StateChanged += new EventHandler(MainWindow_StateChanged);

    }

    void icon_BalloonTipClicked(object sender, EventArgs e)
    {
        this.Show(); //This is where the error is 
        window1.WindowState = WindowState.Normal;
    }

    void icon_DoubleClick(object sender, EventArgs e)
    {
        this.Show(); //This is where the error is 
        window1.WindowState = WindowState.Normal;
    }

    void icon_BalloonTipClosed(object sender, EventArgs e)
    {
        on = false;
    }

    void icon_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {

        if (!on)
        {
            icon.BalloonTipText = "";
            foreach(string item in food){
                if (!item.Contains("Breakfast") && !item.Contains("Soups") && !item.Contains("Vegetables"))
                {
                    icon.BalloonTipText += item+"\n";
                }
            }
            icon.ShowBalloonTip(10);
            on = true;
        }

    }

    void MainWindow_StateChanged(object sender, EventArgs e)
    {
        if (window1.WindowState == WindowState.Minimized)
        {
            this.Hide();
        }
    }

    private void icon_Click(Object sender, EventArgs e)
    {
        icon.BalloonTipText = "";
        foreach (string item in food)
        {
            if (!item.Contains("Breakfast") && !item.Contains("Soups") && !item.Contains("Vegetables"))
            {
                icon.BalloonTipText += item + "\n";
            }
        }
        icon.ShowBalloonTip(10);
        on = true;

    }        
}
like image 200
Anthony Russell Avatar asked Feb 19 '13 23:02

Anthony Russell


People also ask

Where is window system tray?

The notification area (also called the "system tray") is located in the Windows Taskbar, usually at the bottom right corner. It contains miniature icons for easy access to system functions such as antivirus settings, printer, modem, sound volume, battery status, and more.

How do I access system tray icons?

Step 1 − Go to the SETTINGS window and choose System. Step 2 − In the SYSTEM window, select Notifications & actions. Here you can select the option that reads “Select which icons appear on the taskbar”.

How do I open a program from the tray?

Opening a Program from the Taskbar To open a program from the taskbar, simply click on the program icon.


1 Answers

Intercept the Closing event of the window, and cancel it (this will prevent the window from closing) - then hide the window instead:

    public MainWindow()
    {
        // Subscribe to closing event (when X is pressed)
        this.Closing += MainWindow_Closing;
        InitializeComponent();
    }

    void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        // Prevent window from closing
        e.Cancel = true;

        // Hide window
        this.Hide();
    }
like image 81
Blachshma Avatar answered Oct 13 '22 05:10

Blachshma