Problem
What must I do in order for a Windows Forms application to run in the System Tray?
Not a tray program that can be minimized, but a tray application that simply exists in the tray and does nothing else.
Asked by xyz
Solution #1
Creating a Tasktray Application is a code project article that provides a basic explanation and example of how to create an application that only appears in the System Tray.
Basically, the application should be changed. Instead of using the Run(new Form1()); line in Program.cs, create a class that derives from ApplicationContext and have its constructor 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();
}
}
Answered by Fawzan Izy
Solution #2
As mat1t mentioned, you’ll need to include a NotifyIcon in your app and then use code like this 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 merely displays the icon in the system tray:
this.notifyIcon.Visible = true; // Shows the notify icon in the system tray
If you have a form (for whatever reason), you’ll need the following:
this.ShowInTaskbar = false; // Removes the application from the taskbar
Hide();
The context menu is opened automatically when you right-click, but if you want to do something with 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;
}
}
Answered by ChrisF
Solution #3
I used.NET 1.1 to create a traybar application that didn’t require a form. To begin, make the project’s starter object a Sub Main declared in a module. Then programmatically create the NotifyIcon and ContextMenu components. Make sure to include a MenuItem that says “Quit” or something similar. Bind the NotifyIcon to the ContextMenu. Activate the application. Run(). Make sure to use set NotifyIcon in the event handler for the Quit MenuItem. If Visible is False, then Application will be used. Exit(). Add the items you require to the ContextMenu and handle them properly:)
Answered by M.Turrini
Solution #4
Answered by Wolf5
Solution #5
The “System tray” application is a typical Win Forms program with the exception that it creates a system tray icon. Use the NotifyIcon component, which can be found in Toolbox(Common controls), to build the sys.tray icon. Modify its properties: Icon, tool tip. You can also handle mouse click and double click messages with it.
Also, in order to obtain the appearance and feel of a normal tray app. on your main form display event, add the following lines:
private void MainForm_Shown(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
Hide();
}
Answered by Gordon Freeman
Post is based on https://stackoverflow.com/questions/995195/how-can-i-make-a-net-windows-forms-application-that-only-runs-in-the-system-tra