
Does exactly what it says on the tin.
// this variable goes in the main scope of your class
NotifyIcon TrayIcon = new NotifyIcon(); // create the icon to show in the tray
// put this in the constructor for your form
this.TrayIcon.Icon = new Icon(".\\icon.ico"); // apply an icon to it
this.TrayIcon.Visible = false; // make sure it isn't displaying by default
this.Resize += new EventHandler(Form_Resize); // add a resize event
this.TrayIcon.MouseDoubleClick += new MouseEventHandler(TrayIcon_MouseDoubleClick);
// then put these 2 methods in your class
void Form_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{ // if the form has been minimised
this.Hide(); // hide the form
this.TrayIcon.Visible = true; // display the tray icon
}
}
void TrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.TrayIcon.Visible = false; // hide the tray icon
this.Show(); // display the window
this.WindowState = FormWindowState.Normal; // reset the form state from minimised
}
/** EXAMPLE USAGE **/
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DIC
{
public partial class MainForm : Form
{
// this variable goes in the main scope of your class
NotifyIcon TrayIcon = new NotifyIcon(); // create the icon to show in the tray
public MainForm()
{
InitializeComponent();
this.Icon = new Icon(".\\icon.ico");
// put this in the constructor for your form
this.TrayIcon.Icon = new Icon(".\\icon.ico"); // apply an icon to it
this.TrayIcon.Visible = false; // make sure it isn't displaying by default
this.Resize += new EventHandler(Form_Resize); // add a resize event
this.TrayIcon.MouseDoubleClick += new MouseEventHandler(TrayIcon_MouseDoubleClick);
}
void Form_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{ // if the form has been minimised
this.Hide(); // hide the form
this.TrayIcon.Visible = true; // display the tray icon
}
}
void TrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.TrayIcon.Visible = false; // hide the tray icon
this.Show(); // display the window
this.WindowState = FormWindowState.Normal; // reset the form state from minimised
}
}
}-Offline- |