Tutorial scope
- H
- ow to make a tray icon
- How to make your application minimize to the tray
- How to add a context menu to the tray icon(but I won't explain the context menu itself here)
- How to show messages from the tray icon through a balloon
- Make a new Windows Forms Application and give it a name
- Go to ToolBox, and select NotifyIcon (under the Common Controls)
- Drag & Drop it to your form
You will see NotifyIcon1 showing up in a gray box under your form - Find an ICO file (we need to add an icon to show in the system tray)
- Right click on NotifyIcon1, select properties and then under Icon add the icon (It only accepts .ICO files)
How to minimize the application to system tray?
When minimize button is clicked, the program minimizes to system tray and the tray icon shows up. Double clicking on the tray icon will show the form again and remove the icon.
- Set Visible property to false in the property editor of NotifyIcon
- Go to property editor of Form1, click on the little lightning symbol to access the events and double click on the Resize event, and insert the code as follows.
Code: Select all
private void Form1_Resize(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Minimized)//this code gets fired on every resize so we check if the form was minimized { Hide();//hides the program on the taskbar notifyIcon1.Visible = true;//shows our tray icon } }
- Now we need the code to make the program show up again when the icon is double clicked. So double click on NotifyIcon1 in the designer, to make the event handler for the double_click event show up and insert the following code.
Code: Select all
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { Show();//shows the program on taskbar this.WindowState = FormWindowState.Normal;//undoes the minimized state of the form notifyIcon1.Visible = false;//hides tray icon again }
How to add a context menu to the tray icon?
- Drag & Drop ContextMenuStrip under Menus and toolbars form
- Add the menu items as you need
- Go to the property editor of NotifyIcon and select contextMenuStrip1 for ContextMenuStrip property
You might have noticed the little balloons popping up from the system tray with information about things that have happened while the program was running in the background. You can specify a title, text, and pick an icon from a list of three possible ones(Info, Warning, Error, or no icon). There are basically 2 ways of doing this:
By setting the BalloonTipIcon, BalloonTipText and BalloonTipTitle and then calling:
Code: Select all
notifyIcon1.ShowBalloonTip(1000); //1000 is the time in milliseconds the balloon will show up
Code: Select all
ShowBalloonTip(Int32, String, String, ToolTipIcon)
Code: Select all
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
Hide();
notifyIcon1.Visible = true;
//shows a balloon for 1 sec with a title, some text, and the info icon
//other possibilities are: TooltipIcon.None, Tooltipicon.Error, and TooltipIcon.Warning
notifyIcon1.ShowBalloonTip(1000, "Hello", "This is a balloontip!", ToolTipIcon.Info);
}
}
Example application is attached here.