Created
April 29, 2016 11:40
-
-
Save jaywick/dee31f22d4482610a8fb70eabb3530bd to your computer and use it in GitHub Desktop.
WinForms tray app boilerplate. Full credit to https://alanbondo.wordpress.com/2008/06/22/creating-a-system-tray-app-with-c/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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