Advertisement
Yunga

SysTray.cs

Jan 23rd, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. // Compile with:
  2. // csc /target:winexe SysTray.cs
  3.  
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. namespace MyTrayApp {
  9.     public class SysTrayApp : Form {
  10.         [STAThread]
  11.         public static void Main() {
  12.             Application.Run(new SysTrayApp());
  13.         }
  14.  
  15.         private NotifyIcon  trayIcon;
  16.         private ContextMenu trayMenu;
  17.  
  18.         public SysTrayApp() {
  19.             // Create a simple tray menu with only one item.
  20.             trayMenu = new ContextMenu();
  21.             trayMenu.MenuItems.Add("Exit", OnExit);
  22.  
  23.             // Create a tray icon. In this example we use a standard system icon
  24.             trayIcon      = new NotifyIcon();
  25.             trayIcon.Text = "MyTrayApp";
  26.             trayIcon.Icon = new Icon(SystemIcons.Application, 40, 40);
  27.  
  28.             // Add menu to tray icon and show it.
  29.             trayIcon.ContextMenu = trayMenu;
  30.             trayIcon.Visible     = true;
  31.         }
  32.  
  33.         protected override void OnLoad(EventArgs e) {
  34.             Visible       = false; // Hide form window.
  35.             ShowInTaskbar = false; // Remove from taskbar.
  36.             base.OnLoad(e);
  37.         }
  38.  
  39.         private void OnExit(object sender, EventArgs e) {
  40.             Application.Exit();
  41.         }
  42.  
  43.         protected override void Dispose(bool isDisposing) {
  44.             if (isDisposing) {
  45.                 trayIcon.Dispose(); // Release the icon resource.
  46.             }
  47.             base.Dispose(isDisposing);
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement