0

I have an winforms application, in which Im making it to hide in the tray menu when clicking the close button. I have used trayicon and notifyicon in this. The following is the code

For minimizing to the system tray

public void MinimizeToTray()
{
    try
    {
        this.WindowState = FormWindowState.Minimized;
        TrayIcon.Visible = true;
        TrayIcon.ShowBalloonTip(1000);
        ShowInTaskbar = false;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

//Load the tray menu
private void LoadTrayMenu()
{
    TrayMenu.Items.Add("Exit");
    TrayMenu.Items[0].Click += new System.EventHandler(this.Dispose_Click);
    TrayIcon.ContextMenuStrip = TrayMenu;
}

//Close the application
private void Dispose_Click(object Sender, EventArgs e)
{
    TrayIcon.Visible = false;
    TrayIcon.Icon = null;
    Application.Exit();
}

The above part works fine for me, ie I can minimize the application to the traymenu, then re-size to the original form, the close it. But when the application is minimized to the system tray, if at that time i press All-Tab i can view the application. Using which the application can be brought back to the original form instead of clicking from the system tray.

I have viewed these examples

How do I minimize a WinForms application to the notification area?

Best way to hide a window from the Alt-Tab program switcher?

But none of them shows how to hide the application from the Alt-Tab switcher when the application is in the system tray.

Im fine if the application is viewable in the Alt-Tab switcher when the application is not in the system tray.

Any help would be appreciated

Community
  • 1
  • 1
Vikneshwar
  • 1,029
  • 4
  • 20
  • 38

1 Answers1

0

As stated in my comment, use Show() and Hide(). Create a new Form, add a button, double click the button and add the code of button1_Click. I'm using C# (.NET 4), Visual Studio 2012 on Windows 7 x64.

Works fine. The Windows is neither shown in Alt+Tab nor in Win+Tab.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace TrayIconExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private NotifyIcon icon;
        private void button1_Click(object sender, EventArgs e)
        {
            icon = new NotifyIcon
            {
                Icon = new Icon(@"C:\Program Files (x86)\Winspector\class-icons\#32768.ico"),
                Visible = true
            };
            Hide();
        }
    }
}

I leave the Show() part to you.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222