gtk--最小化到系统托盘
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
很多程序都有系统托盘图标,这个在Gtk上可以使用GtkStatusIcon这个构件完成
我们这里简单地测试一个以最小化到系统托盘的程序
当我们点击最小化按钮时程序窗口会消失,然后会在面板上显示我们的图标,当我们点击面板上的图标时会将窗口显示出来
这里除了使用GtkStatusIcon构件外还有另外一个地方需要注意的就是如何捕获窗口的最小化信号
这个信号是window_state_event信号
GdkEventWindowState
typedef struct
{
GdkEventType type;
GdkWindow *window;
gint8 send_event;
GdkWindowState changed_mask;
GdkWindowState new_window_state;
} GdkEventWindowState;
Generated when the state of a toplevel window changes.
GdkEventType type;
the type of the event (GDK_WINDOW_STATE).
GdkWindow *window;
the window which received the event.
gint8 send_event;
TRUE if the event was sent explicitly (e.g. using XSendEvent).
GdkWindowState changed_mask;
mask specifying what flags have changed.
GdkWindowState new_window_state;
the new window state, a combination of GdkWindowState bits.
enum GdkWindowState
typedef enum
{
GDK_WINDOW_STATE_WITHDRAWN = 1 << 0,
GDK_WINDOW_STATE_ICONIFIED = 1 << 1,
GDK_WINDOW_STATE_MAXIMIZED = 1 << 2,
GDK_WINDOW_STATE_STICKY = 1 << 3,
GDK_WINDOW_STATE_FULLSCREEN = 1 << 4,
GDK_WINDOW_STATE_ABOVE = 1 << 5,
GDK_WINDOW_STATE_BELOW = 1 << 6
} GdkWindowState;
Specifies the state of a toplevel window.
GDK_WINDOW_STATE_WITHDRAWN
the window is not shown.
GDK_WINDOW_STATE_ICONIFIED
the window is minimized.
GDK_WINDOW_STATE_MAXIMIZED
the window is maximized.
GDK_WINDOW_STATE_STICKY
the window is sticky.
GDK_WINDOW_STATE_FULLSCREEN
the window is maximized without decorations.
GDK_WINDOW_STATE_ABOVE
the window is kept above other windows.
GDK_WINDOW_STATE_BELOW
the window is kept below other windows.
下面放代码
#include
void tray_on_click(GtkStatusIcon *tray,gpointer data)
{
gtk_widget_show_all(GTK_WIDGET(data));
gtk_window_present(GTK_WINDOW(data));
gtk_status_icon_set_visible(tray,FALSE);
}
void tray_on_menu(GtkStatusIcon *tray,gpointer data)
{
printf("Popup Menu\n");
}
void hide_window(GtkWidget *widget,GdkEventWindowState *event,gpointer data)
{
if(event->changed_mask == GDK_WINDOW_STATE_ICONIFIED && event->new_window_state == GDK_WINDOW_STATE_ICONIFIED)
{
gtk_widget_hide_all(widget);
gtk_status_icon_set_visible(GTK_STATUS_ICON(data),TRUE);
}
}
int main(int argc,char **argv)
{
GtkWidget *win;
GtkStatusIcon *tray;
gtk_init(&argc,&argv);
win=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(win),GTK_WIN_POS_CENTER);
g_signal_connect(G_OBJECT(win),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
tray=gtk_status_icon_new_from_file("/home/brisk/Pictures/color.png");
//gtk_status_icon_set_from_icon_name(tray,"Test");
gtk_status_icon_set_tooltip_text(tray,"This is Test");
gtk_status_icon_set_visible(tray,FALSE);
g_signal_connect(G_OBJECT(tray),"activate",G_CALLBACK(tray_on_click),win);
g_signal_connect(G_OBJECT(tray),"popup_menu",G_CALLBACK(tray_on_menu),NULL);
g_signal_connect(G_OBJECT(win),"window_state_event",G_CALLBACK(hide_window),tray);
gtk_widget_show_all(win);
gtk_main();
return 0;
}