zoukankan      html  css  js  c++  java
  • (转)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.ResponseId==ResponseType.Cancel || e.ResponseId==ResponseType.DeleteEvent)
    aboutDialog.Destroy();
    }

    }

  • 相关阅读:
    Python3基础 keyword 查看所有的关键字
    Python3基础 print 格式化输出 %% 输出%
    Python3基础 print 格式化输出 %f %d 保留浮点数的位数 整数的位数不够零来凑
    Python3基础 for-else break、continue跳出循环示例
    Python3基础 global 在函数内部对全局变量进行修改
    Python3基础 continue while循环示例
    Python3基础 def 函数要先定义再调用
    Python3基础 输出逐行递增的小星星
    Python3基础 九九乘法表
    C#调用接口返回json数据中含有双引号 或其他非法字符的解决办法
  • 原文地址:https://www.cnblogs.com/weiqi/p/1964775.html
Copyright © 2011-2022 走看看