现在有这么一个需求,需要将xfce工具栏上图标的背景设置成指定的背景,那么可以用下面几种方法:
1.修改~/.gtkrc-2.0,这个方法最简单,可以参考:
http://wiki.archlinux.org/index.php/Xfce_%28%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87%29#.E5.A6.82.E4.BD.95.E8.AE.A9.E6.A1.8C.E9.9D.A2.E5.9B.BE.E6.A0.87.E6.A0.87.E7.AD.BE.E5.AD.97.E4.BD.93.E8.83.8C.E6.99.AF.E9.80.8F.E6.98.8E
2.
gtk_widget_set_app_paintable (GTK_WIDGET (item), TRUE); //设定控件显示时的回调函数 g_signal_connect(item, "expose-event",G_CALLBACK(transparent_expose), NULL)
static gboolean transparent_expose (GtkWidget *item, GdkEventExpose *event) { GdkPixmap *gdk_pixmap; GdkPixbuf *gdk_pixbuf,*gdk_pixbuf_tmp; GdkBitmap *mask; gint w, h; gtk_window_get_size (GTK_WINDOW (item), &w, &h); gdk_pixbuf_tmp = gdk_pixbuf_new_from_file (“./background.png”, NULL); gdk_pixbuf = gdk_pixbuf_scale_simple (gdk_pixbuf_tmp, w, h, GDK_INTERP_BILINEAR); g_object_unref (G_OBJECT(gdk_pixbuf_tmp)); gdk_pixbuf_render_pixmap_and_mask (gdk_pixbuf, &gdk_pixmap,&mask, 255); gdk_window_set_back_pixmap (GTK_WIDGET (item)->window, gdk_pixmap, FALSE); gtk_widget_shape_combine_mask (GTK_WIDGET (item), mask, 0, 0); //将widget->window背景设定为 ./background.png }
或者
static gboolean transparent_expose (GtkWidget *widget, GdkEventExpose *event) { GdkPixbuf *gdk_pixbuf, *gdk_pixbuf_tmp; GdkPixmap *gdk_pixmap; gdk_pixbuf_tmp = gdk_pixbuf_new_from_file("./background.png", NULL); gdk_pixbuf = gdk_pixbuf_scale_simple (gdk_pixbuf_tmp, widget->allocation.width, widget->allocation.height , GDK_INTERP_BILINEAR); gdk_pixbuf_render_pixmap_and_mask ( gdk_pixbuf, &gdk_pixmap,NULL, 127); g_object_unref(G_OBJECT(gdk_pixbuf_tmp)); g_object_unref(G_OBJECT(gdk_pixbuf)); gdk_draw_drawable (widget->window, widget->style->black_gc, gdk_pixmap, 0, 0, widget->allocation.x, widget->allocation.y, widget->allocation.width, widget->allocation.height ); g_object_unref(G_OBJECT(gdk_pixmap)); return FALSE; }
3.
static void update_background(GtkWidget *widget, gchar *img_file, gint size) { GdkPixbuf *style_pixbuf_tmp, *style_pixbuf; GdkPixmap *style_pixmap; GtkStyle *style; // 从文件读取背景图 style_pixbuf_tmp = gdk_pixbuf_new_from_file(img_file, NULL); gint pic_width, pic_height; gboolean pic_alpha; pic_width = gdk_pixbuf_get_width(style_pixbuf_tmp); pic_height = gdk_pixbuf_get_height(style_pixbuf_tmp); pic_alpha = gdk_pixbuf_get_has_alpha(style_pixbuf_tmp); style_pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, pic_alpha, 8, size, size + 2); // 将style_pixbuf_tmp复制到style_pixbuf gdk_pixbuf_scale(style_pixbuf_tmp, style_pixbuf, 0, 0, size, size + 2, 0, -2, (double)size/pic_width, (double)(size + 4)/pic_height, GDK_INTERP_BILINEAR); g_object_unref(style_pixbuf_tmp); // 从style_pixbuf,获取style_pixmap gdk_pixbuf_render_pixmap_and_mask(style_pixbuf, &style_pixmap, NULL, 0); g_object_unref(style_pixbuf); // 获得控件的初始style style = gtk_style_copy(GTK_WIDGET (widget)->style); if (style->bg_pixmap[GTK_STATE_NORMAL]) g_object_unref(G_OBJECT(style->bg_pixmap[GTK_STATE_NORMAL])); // 将style_pixmap画到控件背景上 style->bg_pixmap[GTK_STATE_NORMAL] = g_object_ref(style_pixmap); gtk_widget_set_style(GTK_WIDGET (widget), style); g_object_unref(style_pixmap); }
另外,在GNOME开发手册中,对于expose-event事件中实现图片背景的例子也可以借鉴一下:
http://library.gnome.org/devel/gdk-pixbuf/stable/gdk-pixbuf-scaling.html#gdk-pixbuf-scale
gboolean expose_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data) { GdkPixbuf *dest; dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, event->area.width, event->area.height); gdk_pixbuf_composite_color (pixbuf, dest, 0, 0, event->area.width, event->area.height, -event->area.x, -event->area.y, (double) widget->allocation.width / gdk_pixbuf_get_width (pixbuf), (double) widget->allocation.height / gdk_pixbuf_get_height (pixbuf), GDK_INTERP_BILINEAR, 255, event->area.x, event->area.y, 16, 0xaaaaaa, 0x555555); gdk_draw_pixbuf (widget->window, widget->style->fg_gc[GTK_STATE_NORMAL], dest, 0, 0, event->area.x, event->area.y, event->area.width, event->area.height, GDK_RGB_DITHER_NORMAL, event->area.x, event->area.y); gdk_pixbuf_unref (dest); return TRUE; }
函数原型,以及参数的含义:
void gdk_pixbuf_scale (const GdkPixbuf *src, GdkPixbuf *dest, int dest_x,//目的图片的偏移量 int dest_y, int dest_width,//源图片复制到目的图片的宽度 int dest_height, double offset_x,//源图片的偏移量 double offset_y, double scale_x,//要复制的源图片倍数 double scale_y, GdkInterpType interp_type);
Figure 1. Compositing of pixbufs
|
a GdkPixbuf |
|
the GdkPixbuf into which to render the results |
|
the left coordinate for region to render |
|
the top coordinate for region to render |
|
the width of the region to render |
|
the height of the region to render |
|
the offset in the X direction (currently rounded to an integer) |
|
the offset in the Y direction (currently rounded to an integer) |
|
the scale factor in the X direction |
|
the scale factor in the Y direction |
|
the interpolation type for the transformation. |
|
overall alpha for source image (0..255) |