,转Creating a GTK notification area icon using Mono and C#

转载自:http://www.dijksterhuis.org/creating-gtk-notification-area-icon-mono/

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:

// The message to show when the mouse hovers over the icon

myStatusIcon.Tooltip = "Hello World ToolTip";

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.Response| e.ResponseId==ResponseType.DeleteEvent)

aboutDialog.Destroy();

}

}