Created
February 7, 2018 17:08
-
-
Save st0le/b587211c0715e321d419eb97c6030600 to your computer and use it in GitHub Desktop.
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; | |
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