Use a NotifyIcon in C#

example

Some programs use a NotifyIcon in the system tray (typically on the right of the task bar) to display information for the user and to make a popup menu easier to find. The NotifyIcon component makes using such an icon relatively easy.

The component’s Icon property determines the icon that it displays. You can change this property at run time to show the application’s status.

The component’s Text property determines what it displays in its tool tip.

The NotifyIcon also has a ContextMenuStrip property that determines what context menu it displays if the user right-clicks on it.

See Give an application beautiful icons in C# for information on building attractive icons. For this example, I used Inkscape to build happy and sad PNG files with sizes 16×16, 32×32, 48×48, and 256×256 pixels. I then used @icon sushi to convert those images into icons.

Unfortunately I couldn’t figure out how to make the NotifyIcon use the 16×16 pixel images. It seems like it was picking the largest (256×256 pixel) image and scaling it down to 16×16 pixels, and that produced an ugly result. To work around this, I used @icon sushi to build two more icons that contain only the 16×16 pixel images. The program sets the form’s icon to the multi-icon containing all image sizes and it sets the NotifyIcon‘s icon to the 16×16 pixel only icon.

I have read that some systems using pixel scaling may use 20×20 pixel icons in the system tray. In that case, the program will probably scale the 16×16 pixel icon, and that may produce an ugly result. For now, I’m going to leave this example alone, although you could probably ask the system how big these icons should be and then pick an appropriate icon resource. For more information about this, see
Which format and size of icons to use for NotifyIcon at Stack Overflow.

When you click one of the example’s radio buttons, the following code displays the appropriate icons and tool tip.

// Set the happy status.
// The RadioButton and ContextMenu use the same event handler.
private void Happy_Click(object sender, EventArgs e)
{
    this.Icon = Properties.Resources.Happy;
    nicoStatus.Icon = Properties.Resources.Happy16x16;
    nicoStatus.Text = "Status: Happy";
}

// Set the sad status.
// The RadioButton and ContextMenu use the same event handler.
private void Sad_Click(object sender, EventArgs e)
{
    this.Icon = Properties.Resources.Sad;
    nicoStatus.Icon = Properties.Resources.Sad16x16;
    nicoStatus.Text = "Status: Sad";
}

The program uses these same two event handlers both for the radio buttons on its form and for the Happy and Sad items in the NotifyIcon‘s context menu.

The form’s ShowInTaskbar property is false so the program doesn’t appear in the taskbar. Instead you can use the NotifyIcon‘s context menu item Restore to restore the form after you minimize it. This method makes a form only appear in the tray.

That menu item uses the following code to restore the form.

// Restore the form.
private void ctxRestore_Click(object sender, EventArgs e)
{
    this.WindowState = FormWindowState.Normal;
}

The Exit menu item simply closes the form.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in controls, user interface and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *