设定xfce工具栏图标背景的几种方法

来源:互联网 发布:java基础入门 编辑:程序博客网 时间:2024/05/20 07:59

现在有这么一个需求,需要将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.

C代码  收藏代码
  1. gtk_widget_set_app_paintable (GTK_WIDGET (item), TRUE);   
  2.        //设定控件显示时的回调函数  
  3. g_signal_connect(item, "expose-event",G_CALLBACK(transparent_expose), NULL)  
 
C代码  收藏代码
  1. static gboolean  
  2. transparent_expose (GtkWidget *item,   
  3.                     GdkEventExpose *event)  
  4. {  
  5.   
  6.        GdkPixmap *gdk_pixmap;  
  7.        GdkPixbuf *gdk_pixbuf,*gdk_pixbuf_tmp;  
  8.        GdkBitmap *mask;  
  9.        gint w, h;  
  10.   
  11.        gtk_window_get_size (GTK_WINDOW (item), &w, &h);  
  12.        gdk_pixbuf_tmp =  gdk_pixbuf_new_from_file (“./background.png”, NULL);  
  13.   
  14.        gdk_pixbuf =   gdk_pixbuf_scale_simple (gdk_pixbuf_tmp, w, h, GDK_INTERP_BILINEAR);  
  15.        g_object_unref (G_OBJECT(gdk_pixbuf_tmp));  
  16.        gdk_pixbuf_render_pixmap_and_mask (gdk_pixbuf, &gdk_pixmap,&mask, 255);  
  17.   
  18.        gdk_window_set_back_pixmap (GTK_WIDGET (item)->window, gdk_pixmap, FALSE);  
  19.        gtk_widget_shape_combine_mask (GTK_WIDGET (item), mask, 0, 0);  
  20.        //将widget->window背景设定为    ./background.png  
  21. }  

  或者

C代码  收藏代码
  1. static gboolean  
  2. transparent_expose (GtkWidget *widget,   
  3.                     GdkEventExpose *event)  
  4.   
  5. {  
  6.     GdkPixbuf *gdk_pixbuf, *gdk_pixbuf_tmp;  
  7.     GdkPixmap *gdk_pixmap;  
  8.     gdk_pixbuf_tmp =  gdk_pixbuf_new_from_file("./background.png", NULL);  
  9.     gdk_pixbuf =   gdk_pixbuf_scale_simple (gdk_pixbuf_tmp, widget->allocation.width, widget->allocation.height , GDK_INTERP_BILINEAR);  
  10.     gdk_pixbuf_render_pixmap_and_mask ( gdk_pixbuf, &gdk_pixmap,NULL, 127);  
  11.     g_object_unref(G_OBJECT(gdk_pixbuf_tmp));  
  12.     g_object_unref(G_OBJECT(gdk_pixbuf));  
  13.     gdk_draw_drawable (widget->window,  
  14.             widget->style->black_gc,  
  15.             gdk_pixmap,  
  16.             0,  
  17.             0,   
  18.             widget->allocation.x,  
  19.             widget->allocation.y,  
  20.             widget->allocation.width,  
  21.             widget->allocation.height );  
  22.     g_object_unref(G_OBJECT(gdk_pixmap));  
  23.   
  24.     return FALSE;  
  25.   
  26. }  
 

 

3.

C代码  收藏代码
  1. static void  
  2. update_background(GtkWidget *widget, gchar *img_file, gint size)  
  3. {  
  4.     GdkPixbuf *style_pixbuf_tmp, *style_pixbuf;  
  5.     GdkPixmap *style_pixmap;  
  6.     GtkStyle *style;   
  7.           
  8.         // 从文件读取背景图  
  9.     style_pixbuf_tmp = gdk_pixbuf_new_from_file(img_file, NULL);  
  10.   
  11.     gint pic_width, pic_height;  
  12.     gboolean pic_alpha;  
  13.     pic_width =  gdk_pixbuf_get_width(style_pixbuf_tmp);  
  14.     pic_height =  gdk_pixbuf_get_height(style_pixbuf_tmp);  
  15.     pic_alpha =  gdk_pixbuf_get_has_alpha(style_pixbuf_tmp);  
  16.     style_pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, pic_alpha, 8, size, size + 2);   
  17.         // 将style_pixbuf_tmp复制到style_pixbuf  
  18.     gdk_pixbuf_scale(style_pixbuf_tmp, style_pixbuf, 0, 0, size, size + 2,   
  19.                 0, -2, (double)size/pic_width, (double)(size + 4)/pic_height,  
  20.                 GDK_INTERP_BILINEAR);  
  21.     g_object_unref(style_pixbuf_tmp);  
  22.         // 从style_pixbuf,获取style_pixmap  
  23.     gdk_pixbuf_render_pixmap_and_mask(style_pixbuf, &style_pixmap, NULL, 0);  
  24.     g_object_unref(style_pixbuf);  
  25.       
  26.         // 获得控件的初始style  
  27.     style = gtk_style_copy(GTK_WIDGET (widget)->style);  
  28.     if (style->bg_pixmap[GTK_STATE_NORMAL])  
  29.       g_object_unref(G_OBJECT(style->bg_pixmap[GTK_STATE_NORMAL]));  
  30.         // 将style_pixmap画到控件背景上  
  31.     style->bg_pixmap[GTK_STATE_NORMAL] = g_object_ref(style_pixmap);  
  32.     gtk_widget_set_style(GTK_WIDGET (widget), style);  
  33.     g_object_unref(style_pixmap);  
  34.   
  35. }  
 

 

另外,在GNOME开发手册中,对于expose-event事件中实现图片背景的例子也可以借鉴一下:

http://library.gnome.org/devel/gdk-pixbuf/stable/gdk-pixbuf-scaling.html#gdk-pixbuf-scale

C代码  收藏代码
  1. gboolean  
  2. expose_cb (GtkWidget *widget, GdkEventExpose *event, gpointer data)  
  3. {  
  4.   GdkPixbuf *dest;  
  5.   dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, event->area.width, event->area.height);  
  6.   gdk_pixbuf_composite_color (pixbuf, dest,  
  7.                               0, 0, event->area.width, event->area.height,  
  8.                               -event->area.x, -event->area.y,  
  9.                               (double) widget->allocation.width / gdk_pixbuf_get_width (pixbuf),  
  10.                               (double) widget->allocation.height / gdk_pixbuf_get_height (pixbuf),  
  11.                               GDK_INTERP_BILINEAR, 255,  
  12.                               event->area.x, event->area.y, 16, 0xaaaaaa, 0x555555);  
  13.   gdk_draw_pixbuf (widget->window, widget->style->fg_gc[GTK_STATE_NORMAL], dest,  
  14.                    0, 0, event->area.x, event->area.y,  
  15.                    event->area.width, event->area.height,  
  16.                    GDK_RGB_DITHER_NORMAL, event->area.x, event->area.y);  
  17.   gdk_pixbuf_unref (dest);  
  18.   return TRUE;  
  19. }  
 

 

函数原型,以及参数的含义:

C代码  收藏代码
  1. void                gdk_pixbuf_scale                    (const GdkPixbuf *src,  
  2.                                                          GdkPixbuf *dest,  
  3.                                                          int dest_x,//目的图片的偏移量  
  4.                                                          int dest_y,  
  5.                                                          int dest_width,//源图片复制到目的图片的宽度  
  6.                                                          int dest_height,  
  7.                                                          double offset_x,//源图片的偏移量  
  8.                                                          double offset_y,  
  9.                                                          double scale_x,//要复制的源图片倍数  
  10.                                                          double scale_y,  
  11.                                                          GdkInterpType interp_type);  

 

 

Figure 1. Compositing of pixbufs

Compositing of pixbufs


 

src :

a GdkPixbuf

dest :

the GdkPixbuf into which to render the results

dest_x :

the left coordinate for region to render

dest_y :

the top coordinate for region to render

dest_width :

the width of the region to render

dest_height :

the height of the region to render

offset_x :

the offset in the X direction (currently rounded to an integer)

offset_y :

the offset in the Y direction (currently rounded to an integer)

scale_x :

the scale factor in the X direction

scale_y :

the scale factor in the Y direction

interp_type :

the interpolation type for the transformation.

overall_alpha :

overall alpha for source image (0..255)
原创粉丝点击