Skip to content

Instantly share code, notes, and snippets.

@st0le
Created February 7, 2018 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save st0le/b587211c0715e321d419eb97c6030600 to your computer and use it in GitHub Desktop.
Save st0le/b587211c0715e321d419eb97c6030600 to your computer and use it in GitHub Desktop.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MyTrayApp
{
public class SysTrayApp : Form
{
[STAThread]
public static void Main()
{
Application.Run(new SysTrayApp());
}
private NotifyIcon trayIcon;
private ContextMenu trayMenu;
public SysTrayApp()
{
// Create a simple tray menu with only one item.
trayMenu = new ContextMenu();
trayMenu.MenuItems.Add("Exit", OnExit);
// Create a tray icon. In this example we use a
// standard system icon for simplicity, but you
// can of course use your own custom icon too.
trayIcon = new NotifyIcon();
trayIcon.Text = "MyTrayApp";
trayIcon.Icon = new Icon(SystemIcons.Application, 40, 40);
// Add menu to tray icon and show it.
trayIcon.ContextMenu = trayMenu;
trayIcon.Visible = true;
}
protected override void OnLoad(EventArgs e)
{
Visible = false; // Hide form window.
ShowInTaskbar = false; // Remove from taskbar.
base.OnLoad(e);
}
private void OnExit(object sender, EventArgs e)
{
Application.Exit();
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
// Release the icon resource.
trayIcon.Dispose();
}
base.Dispose(isDisposing);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment