Skip to content

Instantly share code, notes, and snippets.

@jaywick
Created April 29, 2016 11:40
Show Gist options
  • Save jaywick/dee31f22d4482610a8fb70eabb3530bd to your computer and use it in GitHub Desktop.
Save jaywick/dee31f22d4482610a8fb70eabb3530bd to your computer and use it in GitHub Desktop.
using System;
using System.Drawing;
using System.Windows.Forms;
/// WinForms trap app boilerplate
/// Full credit to @alanbondo:
/// https://alanbondo.wordpress.com/2008/06/22/creating-a-system-tray-app-with-c/
namespace TrayApp
{
public class App : Form
{
[STAThread]
public static void Main()
{
Application.Run(new App());
}
private NotifyIcon _trayIcon;
private ContextMenu _trayMenu;
public App()
{
_trayMenu = new ContextMenu();
_trayMenu.MenuItems.Add("Exit", OnExit);
_trayIcon = new NotifyIcon
{
Text = "MyTrayApp",
Icon = new Icon(SystemIcons.Application, 40, 40),
ContextMenu = _trayMenu,
Visible = true,
};
// TODO: launch your background worker here
}
protected override void OnLoad(EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
base.OnLoad(e);
}
private void OnExit(object sender, EventArgs e)
{
Application.Exit();
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
_trayIcon.Dispose();
base.Dispose(isDisposing);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment