Notify Icon C#

Hi there,

I’ve been building an application that shows that little yellow balloon in the right corner (your system tray). I just wanted to share how I accomplished it.

notify

1. First of all you need to instantiate an object NotifyIcon

You can give NotifyIcon an icon that shows in the system tray by adding any .ico file to a Resource file in your project. You can then reference this resource file. An other important property of the object is the .Visible property. This tells the icon.. if it’s visible in the system tray. You also might want to add a handler to the .DoubleClick. I’ve done this to show a dialog to change settings of the little program in question.

_notifyIcon = new NotifyIcon { 
              Icon = Mail_Checker.Resources.Icons.mail, 
              Visible = false 
                             };
_notifyIcon.DoubleClick += ToggleSettingsDialog;


//The handler
private void ToggleSettingsDialog(object e, EventArgs ea)
{
    if (WindowState == WindowState.Minimized)
    {
        this.Show();
        this.WindowState = WindowState.Normal;
        _notifyIcon.Visible = false;
    }
    else
    {
        this.WindowState = WindowState.Minimized;
        this.Hide();
        _notifyIcon.Visible = true;
    }
}

2. Adding a right-click menu

To add a right-click menu to a notifyicon you have to create two objects. A MenuItem and a ContextMenu.

The contextmenu is the actual menu itself, and every option in that menu has to be added manually. So first, let’s create a quit-item for the menu.

//Initialize quit-option in menu 
_menuItemQuit = new MenuItem
                {
                    Index = 0,
                    Text = "Quit"
                };
_menuItemQuit.Click += MenuItemQuitClick;

Next, we want to add that to the (not yet created) contextmenu so let’s do that now. As you can see, you need to cast the menuitem to an array, since the contextmenu usually takes more than 1 menu item.

//Configure contextmenu for notifyicon
_notifyIconContextMenu = new System.Windows.Forms.ContextMenu();
_notifyIconContextMenu.MenuItems.AddRange(
                                         new[] { _menuItemQuit }
                                         );

And at last add that contextmenu to your notifyicon:

_notifyIcon.ContextMenu = _notifyIconContextMenu;

Now, to show a balloontip during runtime it’s as simple as calling ShowBalloonTip. The parameters below are the time-out (time it will display), the title, the message itself and the icon it shoud be given.

_notifyIcon.Visible = true;
_notifyIcon.ShowBalloonTip
    (5000, "Mail Checker", "Check your inbox, sir!", ToolTipIcon.Info);

Source code for the little program I built can be found on Github. Mail Notifier C# @ Github

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s