How to minimize to System tray using C#

.NET programming topics
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to minimize to System tray using C#

Post by Neo » Wed Dec 23, 2009 9:14 pm

This tutorial will cover the use of the NotifyIcon, better known as the System Tray Icon in C#. The NotifyIcon is a cool feature to add to your application, and you can see numerous examples of it's usage in for example μTorrent, MSN messenger, and your favourite Anti-Virus program.
1.jpg
1.jpg (4.63 KiB) Viewed 6618 times
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
Making the Tray Icon
  1. Make a new Windows Forms Application and give it a name
  2. Go to ToolBox, and select NotifyIcon (under the Common Controls)
    2.jpg
    2.jpg (4.77 KiB) Viewed 6618 times
  3. Drag & Drop it to your form
    You will see NotifyIcon1 showing up in a gray box under your form
  4. Find an ICO file (we need to add an icon to show in the system tray)
  5. Right click on NotifyIcon1, select properties and then under Icon add the icon (It only accepts .ICO files)
Now when running the program you'll see a tray icon showing up. Lets move to next step.

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.
  1. Set Visible property to false in the property editor of NotifyIcon
  2. 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
                }
            }
    
  3. 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
            }
    
Now your program will close to system tray and pops up again when double clicked. You can change the name the user sees when hovering over the icon by changing the Text property.

How to add a context menu to the tray icon?
  1. Drag & Drop ContextMenuStrip under Menus and toolbars form
  2. Add the menu items as you need
  3. Go to the property editor of NotifyIcon and select contextMenuStrip1 for ContextMenuStrip property
How to pass messages through balloons?

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
But a more flexible way is using an overload of ShowBalloonTip:

Code: Select all

ShowBalloonTip(Int32, String, String, ToolTipIcon)
To do this, got to Form1_Resize event we used earlier and add the last few lines as follows.

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);
            }
        }
Now you'll see a balloon is visible as soon as the application is minimized.

Example application is attached here.
SysTray.zip
(14.3 KiB) Downloaded 307 times
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to minimize to System tray using C#

Post by Neofriend » Thu Dec 24, 2009 12:45 am

Superb Neo!
I've read the same kind of tutorial elsewhere and implemented it but your style of presenting it was brilliant!
Furthermore, the best thing I've loved was your info about the BalloonTips.

Looking forward to more! :)
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to minimize to System tray using C#

Post by Neofriend » Thu Dec 24, 2009 2:04 am

Is it possible to create an effect like google talk ballontip? and how they've implemented the logic of showing up new emails.....custom design and logic.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to minimize to System tray using C#

Post by Neo » Thu Dec 24, 2009 2:28 am

Really sorry, I do not use Google Talk. However the ballootip can be triggered whenever you want with the given command.

For Example: notifyIcon1.ShowBalloonTip(1000, "Hello", "This is a balloontip!", ToolTipIcon.Info);
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to minimize to System tray using C#

Post by Neofriend » Thu Dec 24, 2009 2:35 am

No problem, my friend.

I've attached the image, see the 2nd icon from left please. And on clicking, it opens the tab above it. Opens like the window is coming from bottom and slides upwards.
Capture.PNG
Capture.PNG (17.59 KiB) Viewed 6610 times
Last edited by Neo on Sat Jun 04, 2011 9:23 pm, edited 1 time in total.
Reason: Attachment added to post
User avatar
Enigma
Lieutenant
Lieutenant
Posts: 74
Joined: Sun Jan 16, 2011 12:40 am
Location: Colombo, Sri Lanka

Re: How to minimize to System tray using C#

Post by Enigma » Sat Jun 04, 2011 10:19 am

Hi

I know its a bit of old thread. But here goes a simple gtalk window. Its not perfect. But it will give a start.

Enigma
just double click this icon.
just double click this icon.
1.png (9.73 KiB) Viewed 6378 times
2.png
2.png (18.85 KiB) Viewed 6378 times
GtalkWindow.zip
Source Code VS2010
(521.58 KiB) Downloaded 241 times
Last edited by Neo on Sat Jun 04, 2011 9:23 pm, edited 1 time in total.
Reason: Attachment(s) added to post
Post Reply

Return to “.NET Programming”