歡迎您光臨本站 註冊首頁

如何使用gtk讓系統托盤顯示圖標

←手機掃碼閱讀     火星人 @ 2014-03-12 , reply:0
  翻譯介紹:
這篇文章來自http://www.codeproject.com/KB/cross-platform/GTKTrayIcon.aspx。
使用下面的演示代碼時,注意圖標的路徑,否則不能顯示托盤圖標。如果編譯不成功,請注意編譯命令中的字元是否是英文的字元。


介紹:

這篇文章解釋了GtkStatusIcon部件的使用,這個GtkStatusIcon部件用來在系統托盤裡放置圖標。為了和用戶交互提示條和彈出菜單可以加入到這個圖標里。這個圖標也可以閃爍。

如何在系統托盤裡放置圖標:



下列函數可以用來創建一個新的狀態圖標:
    GtkStatusIcon* gtk_status_icon_new ();

    GtkStatusIcon* gtk_status_icon_new_from_pixbuf    (GdkPixbuf *pixbuf);

    GtkStatusIcon* gtk_status_icon_new_from_file      (const gchar *filename);

    GtkStatusIcon* gtk_status_icon_new_from_stock     (const gchar *stock_id);

    GtkStatusIcon* gtk_status_icon_new_from_icon_name (const gchar *icon_name);


為了設置一個提示條文本可以用下面的函數:
 void gtk_status_icon_set_tooltip (GtkStatusIcon *icon, const gchar *tooltip_text);

為了閃爍一個圖標用下面的函數:
void gtk_status_icon_set_blinking (GtkStatusIcon *icon, gboolean blinking);



當用戶激活這個托盤圖標,托盤圖標發送"activate"信號;當用戶指出一個菜單應該顯示時,托盤圖標發送一個"popup-menu"信號。


演示程序:
下面的程序當用戶按下最小化按鈕時,創建一個托盤圖標,隱藏應用窗口。使用托盤圖標的彈出菜單應用窗口變得有效。當和托盤圖標交互時信號被處理。
//trayicon.c
#include <gtk/gtk.h>

static void trayView(GtkMenuItem *item, gpointer user_data);
static void trayExit(GtkMenuItem *item, gpointer user_data);
static void trayIconActivated(GObject *trayIcon, gpointer data);
static void trayIconPopup(GtkStatusIcon *status_icon, guint button, guint32 activate_time, gpointer popUpMenu);
static void destroy (GtkWidget*, gpointer);
static gboolean delete_event (GtkWidget*, GdkEvent*, gpointer);
static gboolean window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer user_data);

int main(int argc, char *argv[])
{
    gtk_init (&argc, &argv);
   
    GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "GtkStatusIcon Example");
    gtk_widget_set_size_request (window, 200, -1);
   
    //set try icon file
    GtkStatusIcon *trayIcon  = gtk_status_icon_new_from_file ("/root/Desktop/icon.png");
    //set popup menu for tray icon
    GtkWidget *menu, *menuItemView, *menuItemExit;
    menu = gtk_menu_new();
    menuItemView = gtk_menu_item_new_with_label ("View");
    menuItemExit = gtk_menu_item_new_with_label ("Exit");
    g_signal_connect (G_OBJECT (menuItemView), "activate", G_CALLBACK (trayView), window);
    g_signal_connect (G_OBJECT (menuItemExit), "activate", G_CALLBACK (trayExit), NULL);
    gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuItemView);
    gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuItemExit);
    gtk_widget_show_all (menu);
    //set tooltip
    gtk_status_icon_set_tooltip (trayIcon, "MsgWatcherGTK");
    //connect handlers for mouse events
    g_signal_connect(GTK_STATUS_ICON (trayIcon), "activate", GTK_SIGNAL_FUNC (trayIconActivated), window);
    g_signal_connect(GTK_STATUS_ICON (trayIcon), "popup-menu", GTK_SIGNAL_FUNC (trayIconPopup), menu);
    gtk_status_icon_set_visible(trayIcon, FALSE); //set icon initially invisible
   
   
    GtkWidget *menuBar, *menuItemTopLvl, *mainMenu, *mainMenuItemExit;
    menuBar = gtk_menu_bar_new ();
    menuItemTopLvl = gtk_menu_item_new_with_label ("Menu");
    gtk_menu_shell_append (GTK_MENU_SHELL (menuBar), menuItemTopLvl);
   
    mainMenu = gtk_menu_new ();
    gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuItemTopLvl), mainMenu);
    mainMenuItemExit = gtk_menu_item_new_with_label ("Quit");
    g_signal_connect (G_OBJECT (mainMenuItemExit), "activate", G_CALLBACK (trayExit), NULL);
    gtk_menu_shell_append (GTK_MENU_SHELL (mainMenu), mainMenuItemExit);
   
    g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy), NULL);
    g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (delete_event), trayIcon);
    g_signal_connect (G_OBJECT (window), "window-state-event", G_CALLBACK (window_state_event), trayIcon);
    gtk_container_add (GTK_CONTAINER (window), menuBar);
    gtk_widget_show_all (window);
    gtk_main ();
    return 0;
}

static void trayView(GtkMenuItem *item, gpointer window)
{
    gtk_widget_show(GTK_WIDGET(window));
    gtk_window_deiconify(GTK_WINDOW(window));   
}

static void trayExit(GtkMenuItem *item, gpointer user_data)
{
    printf("exit");
    gtk_main_quit();
}

static void trayIconActivated(GObject *trayIcon, gpointer window)
{
    gtk_widget_show(GTK_WIDGET(window));
    gtk_window_deiconify(GTK_WINDOW(window));
}

static void trayIconPopup(GtkStatusIcon *status_icon, guint button, guint32 activate_time, gpointer popUpMenu)
{
    gtk_menu_popup(GTK_MENU(popUpMenu), NULL, NULL, gtk_status_icon_position_menu, status_icon, button, activate_time);
}

static void destroy (GtkWidget *window, gpointer data)
{
  gtk_main_quit ();
}

static gboolean delete_event (GtkWidget *window, GdkEvent *event, gpointer data)
{
    return FALSE;
}

static gboolean window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer trayIcon)
{
    if(event->changed_mask == GDK_WINDOW_STATE_ICONIFIED && (event->new_window_state == GDK_WINDOW_STATE_ICONIFIED || event->new_window_state == (GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_MAXIMIZED)))
    {
        gtk_widget_hide (GTK_WIDGET(widget));
        gtk_status_icon_set_visible(GTK_STATUS_ICON(trayIcon), TRUE);
    }
    else if(event->changed_mask == GDK_WINDOW_STATE_WITHDRAWN && (event->new_window_state == GDK_WINDOW_STATE_ICONIFIED || event->new_window_state == (GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_MAXIMIZED)))
    {
        gtk_status_icon_set_visible(GTK_STATUS_ICON(trayIcon), FALSE);
    }
    return TRUE;
}

上面的代碼可以用下面的命令編譯:

gcc trayicon.c -o trayicon `pkg-config --cflags --libs gtk+-2.0`.
License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


[火星人 ] 如何使用gtk讓系統托盤顯示圖標已經有493次圍觀

http://coctec.com/docs/program/show-post-71647.html