What do I need to do to make a Windows Forms application to be able to run in the System Tray?
Not an application that can be minimized to the tray, but an application that will be only exist in the tray, with nothing more than
When any window is open, press Win + Shift + Z to minimize the window to the system tray. The program's icon does show up in the tray. To maximize the window and return it to the taskbar, double-click the program's icon in the system tray.
This can be done by doing the following in your form's Resize event handler: Check whether the form's WindowState property is set to FormWindowState. Minimized. If yes, hide your form, enable the NotifyIcon object, and show the balloon tip that shows some information. Once the WindowState becomes FormWindowState.
Press Alt + F1 and that window will minimize to the tray.
The code project article Creating a Tasktray Application gives a very simple explanation and example of creating an application that only ever exists in the System Tray.
Basically change the Application.Run(new Form1());
line in Program.cs
to instead start up a class that inherits from ApplicationContext
, and have the constructor for that class initialize a NotifyIcon
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyCustomApplicationContext());
}
}
public class MyCustomApplicationContext : ApplicationContext
{
private NotifyIcon trayIcon;
public MyCustomApplicationContext ()
{
// Initialize Tray Icon
trayIcon = new NotifyIcon()
{
Icon = Resources.AppIcon,
ContextMenu = new ContextMenu(new MenuItem[] {
new MenuItem("Exit", Exit)
}),
Visible = true
};
}
void Exit(object sender, EventArgs e)
{
// Hide tray icon, otherwise it will remain shown until user mouses over it
trayIcon.Visible = false;
Application.Exit();
}
}
As mat1t says - you need to add a NotifyIcon to your application and then use something like the following code to set the tooltip and context menu:
this.notifyIcon.Text = "This is the tooltip";
this.notifyIcon.ContextMenu = new ContextMenu();
this.notifyIcon.ContextMenu.MenuItems.Add(new MenuItem("Option 1", new EventHandler(handler_method)));
This code shows the icon in the system tray only:
this.notifyIcon.Visible = true; // Shows the notify icon in the system tray
The following will be needed if you have a form (for whatever reason):
this.ShowInTaskbar = false; // Removes the application from the taskbar
Hide();
The right click to get the context menu is handled automatically, but if you want to do some action on a left click you'll need to add a Click handler:
private void notifyIcon_Click(object sender, EventArgs e)
{
var eventArgs = e as MouseEventArgs;
switch (eventArgs.Button)
{
// Left click to reactivate
case MouseButtons.Left:
// Do your stuff
break;
}
}
I've wrote a traybar app with .NET 1.1 and I didn't need a form.
First of all, set the startup object of the project as a Sub Main
, defined in a module.
Then create programmatically the components: the NotifyIcon
and ContextMenu
.
Be sure to include a MenuItem
"Quit" or similar.
Bind the ContextMenu
to the NotifyIcon
.
Invoke Application.Run()
.
In the event handler for the Quit MenuItem
be sure to call set NotifyIcon.Visible = False
, then Application.Exit()
.
Add what you need to the ContextMenu
and handle properly :)
Form1
from the code. Form1
.NotifyIcon
class to create your system tray icon (assign an icon to it).NotifyIcon
's mouseclick and differenciate between Right and Left click, setting your contextmenu and showing it for which ever button (right/left) was pressed.Application.Run()
to keep the app running with Application.Exit()
to quit. Or a bool bRunning = true; while(bRunning){Application.DoEvents(); Thread.Sleep(10);}
. Then set bRunning = false;
to exit the app."System tray" application is just a regular win forms application, only difference is that it creates a icon in windows system tray area. In order to create sys.tray icon use NotifyIcon component , you can find it in Toolbox(Common controls), and modify it's properties: Icon, tool tip. Also it enables you to handle mouse click and double click messages.
And One more thing , in order to achieve look and feels or standard tray app. add followinf lines on your main form show event:
private void MainForm_Shown(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
Hide();
}
As far as I'm aware you have to still write the application using a form, but have no controls on the form and never set it visible. Use the NotifyIcon (an MSDN sample of which can be found here) to write your application.
Here is how I did it with Visual Studio 2010, .NET 4
It is very friendly framework for Notification Area Application... it is enough to add NotificationIcon to base form and change auto-generated code to code below:
public partial class Form1 : Form
{
private bool hidden = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.ShowInTaskbar = false;
//this.WindowState = FormWindowState.Minimized;
this.Hide();
hidden = true;
}
private void notifyIcon1_Click(object sender, EventArgs e)
{
if (hidden) // this.WindowState == FormWindowState.Minimized)
{
// this.WindowState = FormWindowState.Normal;
this.Show();
hidden = false;
}
else
{
// this.WindowState = FormWindowState.Minimized;
this.Hide();
hidden = true;
}
}
}
I adapted the accepted answer to .NET Core, using the recommended replacements for deprecated classes:
Program.cs
namespace TrayOnlyWinFormsDemo
{
internal static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new MyCustomApplicationContext());
}
}
}
MyCustomApplicationContext.cs
using TrayOnlyWinFormsDemo.Properties; // Needed for Resources.AppIcon
namespace TrayOnlyWinFormsDemo
{
public class MyCustomApplicationContext : ApplicationContext
{
private NotifyIcon trayIcon;
public MyCustomApplicationContext()
{
trayIcon = new NotifyIcon()
{
Icon = Resources.AppIcon,
ContextMenuStrip = new ContextMenuStrip()
{
Items = { new ToolStripMenuItem("Exit", null, Exit) }
},
Visible = true
};
}
void Exit(object? sender, EventArgs e)
{
trayIcon.Visible = false;
Application.Exit();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With