How can I make a .NET Windows Forms application that only runs in the System 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();
}
}
c# create system tray application that remains in tray after application closed
When you exit the application, the tray icon goes away.
Also, when you close the last window, the application exits.
You want to minimize and hide the main window instead of exiting. You can do this with Hide();
somewhere in your Windows Forms code. Then, when the user wants to show the form, just run form.Show()
.
Developing a simple Windows system tray desktop app to consume a .NET web service
Keep it all in .NET. You can easily write a Windows Forms application to display a tray icon and display notifications as and when something happens in the web service (you'd probably need a timer to do the polling).
There are plenty of articles around that will show you how to do this. Here's one to get you started:
http://www.codeproject.com/Articles/290013/Formless-System-Tray-Application
Single Instance Windows Forms Application with Minimize to Tray
Making Single Instance
Add a reference to Microsoft.VisualBasic.dll
to your project and add this class to your project:
using System;
using Microsoft.VisualBasic.ApplicationServices;
using System.Windows.Forms;
namespace Sample
{
public class ApplicationController : WindowsFormsApplicationBase
{
private Form mainForm;
public ApplicationController(Form form)
{
//We keep a reference to main form
//To run and also use it when we need to bring to front
mainForm = form;
this.IsSingleInstance = true;
this.StartupNextInstance += this_StartupNextInstance;
}
void this_StartupNextInstance(object sender, StartupNextInstanceEventArgs e)
{
//Here we bring application to front
e.BringToForeground = true;
mainForm.ShowInTaskbar = true;
mainForm.WindowState = FormWindowState.Minimized;
mainForm.Show();
mainForm.WindowState = FormWindowState.Normal;
}
protected override void OnCreateMainForm()
{
this.MainForm = mainForm;
}
}
}
Then in your Program.cs
use that ApplicationController
to run the program:
using System;
using System.Windows.Forms;
namespace Sample
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//create a controller and Pass an instance of your application main form
var controller = new Sample.ApplicationController(new YourMainForm());
//Run application
controller.Run(Environment.GetCommandLineArgs());
}
}
}
Now your application is Single Instance and when you click on your exe file, instead of running another instance, brings the existing instance to front.
Using NotifyIcon
Put a NotifyIcon
on your main form and then to hide window when you click minimize button and to show windows when you click on notify icon and you can handle these events:
//When click on notify icon, we bring the form to front
private void notifyIcon_Click(object sender, EventArgs e)
{
this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Minimized;
this.Show();
this.WindowState = FormWindowState.Normal;
}
//here we check if the user minimized window, we hide the form
private void ApplicationMainForm_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
this.Hide();
}
}
//when the form is hidden, we show notify icon and when the form is visible we hide it
private void ApplicationMainForm_VisibleChanged(object sender, EventArgs e)
{
this.notifyIcon1.Visible = !this.Visible;
}
Create a program to run from the system tray
I review the answers I note that miss the icon.
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
If Me.WindowState = FormWindowState.Minimized Then
NotifyIcon1.Visible = True
NotifyIcon1.Icon = SystemIcons.Application
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
NotifyIcon1.BalloonTipTitle = "Verificador corriendo"
NotifyIcon1.BalloonTipText = "Verificador corriendo"
NotifyIcon1.ShowBalloonTip(50000)
'Me.Hide()
ShowInTaskbar = False
End If
End Sub
Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
'Me.Show()
ShowInTaskbar = True
Me.WindowState = FormWindowState.Normal
NotifyIcon1.Visible = False
End Sub
minimize app to system tray
- C# System Tray Minimize To Tray With NotifyIcon
- Minimize window to system tray
Handle the form’s Resize event. In this handler, you override the
basic functionality of the Resize event to make the form minimize to
the system tray and not to the taskbar. 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.Normal, disable the NotifyIcon object by setting its
Visible property to false. Now, you want the window to reappear when
you double click on the NotifyIcon object in the taskbar. For this,
handle the NotifyIcon’s MouseDoubleClick event. Here, you show the
form using the Show() method.
private void frmMain_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
mynotifyicon.Visible = true;
mynotifyicon.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
mynotifyicon.Visible = false;
}
}
How to add ONLY system tray icon to application?
You can do it with a custom ApplicationContext. Google reveals first this tutorial on how to achieve it.
Or you can alter your main Program file not to show any form at all:
Application.Run(); //remove the Form oject from this call
Related Topics
How to Use Linq with a MySQL Database on Mono
How to Assign a Base Class Object to a Derived Class Reference with an Explicit Typecast
Why Are C# 3.0 Object Initializer Constructor Parentheses Optional
How to Get the Unix Timestamp in C#
Change Wpf Controls from a Non-Main Thread Using Dispatcher.Invoke
Deserializing JSON into an Object
Xml Serialization of Interface Property
Xml Serialization and Namespace Prefixes
C# Execute a Terminal Command in Linux
Checking for Directory and File Write Permissions in .Net
Conditional Compilation and Framework Targets
Fixed Size Queue Which Automatically Dequeues Old Values Upon New Enques
Can't View Designer When Coding a Form in C#
How to Find Out the Return Value Before Returning While Debugging in Visual Studio