动机:由于要实现一个列表控件,控件的每行不仅有动态获取的文字,而且此行要有背景图片。找了好多方法都没实现想要的效果:
还没有发现如何给treeview或者其单元格设置背景图片,只能设置颜色
pixbufcellrender 和 textcellrender等在加入treeview时都是一个并列的关系,不能进行重叠
所以如果能将行背景图片创建一个pixbuf,然后将每行对应的文字按照一定格式绘制在pixbuf上面,然后通过此pixbuf创建一个pixbufcellrender,将此cellrender加入到treeview中,那么想要的效果就实现了。
想到此赶忙对此想法做了验证---更改创建的gdkpixbuf,测试通过,代码如下:
#include
int main (int argc, char *argv[])
{
GtkWidget *win = NULL;
GtkWidget *vbox = NULL;
GtkWidget *image;
GdkPixbuf *pixbuf;
//Initialize GTK+
gtk_init (&argc, &argv);
//Create the main window
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width (GTK_CONTAINER (win), 0);
gtk_widget_set_usize(win, 193, 63);
gtk_window_set_title (GTK_WINDOW (win), "Test draw rectangle to pixbuf");
gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
g_signal_connect (win, "destroy", gtk_main_quit, NULL);
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_add (GTK_CONTAINER (win), vbox);
//create pixbuf from image file
pixbuf = gdk_pixbuf_new_from_file ("kp_7.png", NULL);
//get pixmap from pixbuf
GdkPixmap *pixmap = NULL;
gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, NULL, 0);
gint w = gdk_pixbuf_get_width(pixbuf);
gint h = gdk_pixbuf_get_height(pixbuf);
cairo_t *cr = gdk_cairo_create(pixmap);
cairo_move_to(cr, 20, 20);
cairo_set_font_size(cr, 14);
cairo_select_font_face(cr,"DongWen--Song", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_show_text(cr, "aaa");
pixbuf = gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);
//create image widget from new pixbuf
image = gtk_image_new_from_pixbuf (pixbuf);
gtk_box_pack_start(GTK_BOX(vbox), image, TRUE, TRUE, 0);
//release
g_object_unref(pixbuf);
g_object_unref(pixmap);
//show
gtk_widget_show_all (win);
gtk_main ();
return 0;
}
此代码成功验证了我的想法,那么treeview中我想要的效果就可以实现了
阅读(2646) | 评论(0) | 转发(0) |