January 2nd, 2009
Creating a GTK notification area icon using Mono and C# - 3
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
Cool (Linux) applications have their own icon in the notification area. To add my own icon in the Gnome notification area using Mono only took a few lines of code. In the post below I show how you can add your icon and associate a tooltip and right click menu to it.
GTK provides the StatusIcon class to register your own icon in the notification area.
StatusIcon myStatusIcon;
You can provide and load your own icon, but for this example I used one of the GTK stock icons.
myStatusIcon = StatusIcon.NewFromStock(Stock.Harddisk); // I use a stock icon from Stock.*
// to avoid having to include an icon file
myStatusIcon.Visible = true; // Make sure we are displayed
Adding or changing a tooltip is as straighforward as assigning it:
myStatusIcon.Tooltip = "Hello World ToolTip"; // The message to show when the mouse hovers over the icon
To create a rightclick menu we need to register the PopupMenu event handler:
myStatusIcon.PopupMenu += OnStatusIconPopupMenu; // Link in the right click popup menu
...
...
/* The Status Popup menu is shown when the user right clicks on the toolbar icon */
protected void OnStatusIconPopupMenu(object sender, EventArgs e)
{
Menu popupMenu = new Menu();
MenuItem helloItem = new MenuItem("About Hello World");
helloItem.Show();
helloItem.Activated += new EventHandler(OnHelloAboutActivated);
popupMenu.Append(helloItem);
popupMenu.Popup(null,null,null,3,Gtk.Global.CurrentEventTime);
}
And that is really all there is to it. I have included the complete code below. If you create a new GTK solution in MonoDevelop you will end up with two files, Main.cs and MainWindows.Cs. Replace the MainWindow.CS file with the content below:
using System;
using Gtk;
public partial class MainWindow: Gtk.Window
{
StatusIcon myStatusIcon;
AboutDialog aboutDialog;
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
/* The following lines create the notification area status icon */
/* Make it visible and link in the right click popup menu */
myStatusIcon = StatusIcon.NewFromStock(Stock.Harddisk); // I use a stock icon from Stock.*
// to avoid having to include an icon file
myStatusIcon.Tooltip = "Hello World ToolTip"; // The message to show when the mouse hovers over the icon
myStatusIcon.Visible = true; // Make sure we are displayed
myStatusIcon.PopupMenu += OnStatusIconPopupMenu; // Link in the right click popup menu
/* Not necessary */
Label AppWindowLabel = new Label("Hello World");
this.Add(AppWindowLabel);
Build ();
}
/* Called when the main application closes */
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
/* The Status Popup menu is shown when the user right clicks on the toolbar icon */
protected void OnStatusIconPopupMenu(object sender, EventArgs e)
{
Menu popupMenu = new Menu();
MenuItem helloItem = new MenuItem("About Hello World");
helloItem.Show();
helloItem.Activated += new EventHandler(OnHelloAboutActivated);
popupMenu.Append(helloItem);
popupMenu.Popup(null,null,null,3,Gtk.Global.CurrentEventTime);
}
/* If the user select the "About Hello World" menu option, we show the about dialog */
protected void OnHelloAboutActivated(object sender, EventArgs e)
{
aboutDialog = new AboutDialog();
aboutDialog.ProgramName = "The About Dialog Popup Demo";
aboutDialog.Version = "1.0beta";
aboutDialog.Comments = "The best things in life are simple!";
aboutDialog.License = "Creative Commons";
aboutDialog.Authors = new string[] { "Martijn Dijksterhuis" };
aboutDialog.Website = "http://www.dijksterhuis.org";
aboutDialog.Response += new ResponseHandler(OnHelloAboutClose);
aboutDialog.Run();
}
/* Catch the "Close" and "X" button event from the about dialog box */
protected void OnHelloAboutClose(object sender, ResponseArgs e)
{
if (e.ResponseId==ResponseType.Cancel || e.ResponseId==ResponseType.DeleteEvent)
aboutDialog.Destroy();
}
}
Tags: GTK, Learn C#, mono, StatusIcon, Tooltip










Except where otherwise noted, content on this site is
February 9th, 2009 at 8:33 pm
Thanks for the tutorial.
However, I would like to add my own icon. How would this be accomplished? What format should the icon be in? Can the icon be stored inside the exe file?
February 11th, 2009 at 1:20 pm
Hi Ralf,
You can use a Gdk.Pixbuf if you would just like to load an external image (Pixbuf will accept (png, tiff, jpg, gif, xpm, pcx, ico, xpm, xbm)
I haven’t yet tried to distribute this in an exe file, so I can’t help you there.
Cheers,
Martijn
November 5th, 2010 at 12:49 pm
Hi Martin,
On Windows the notification icon hangs around after the application quits, at least until you mouse over it. You can fix this behaviour with the following small change to your DeleteEvent handler:
protected void OnDeleteEvent(object sender, DeleteEventArgs a)
{
myStatusIcon.Visible = false; // <– hide icon before quitting
Application.Quit();
a.RetVal = true;
}
Kind regards,
Anthony.