GTK+ 2 给窗口设置背景图片

If you want set a image like a background in you gtk window, you should do:
1 - Load the image to a GdkPixbuf or variant. (gdk_pixbuf_new_from_file)
2 - Create a pixmap and a mask bitmap. (gdk_pixbuf_render_pixmap_and_mask () )
3 - Create a GtkStyle that hold style information for widgets.
4 - Load the render pixmap to the style.
5 - Set the GtkStyle to the main window.

An example:


#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
 
GdkPixbuf *load_pixbuf_from_file (const char *filename)
{
    GError *error = NULL;
    GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file (filename, &error);
 
    if (pixbuf == NULL)
    {
        g_print ("Error loading file: %d : %s\n", error->code, error->message);
        g_error_free (error);
        exit (1);
    }
    return pixbuf;
}
 
GdkPixbufAnimation *load_pixbuf_animation_from_file (const char *filename)
{
    GError *error = NULL;
    GdkPixbufAnimation *pixbuf = gdk_pixbuf_animation_new_from_file (filename, &error);
 
    if (pixbuf == NULL)
    {
        g_print ("Error loading file: %d : %s\n", error->code, error->message);
        g_error_free (error);
        exit (1);  
    }
    return pixbuf;
}
 
int main (int argc, char **argv)
{
    GtkWidget *window = NULL;
    GdkPixbuf *image = NULL;
    GdkPixbufAnimation * anim = NULL;
    GtkWidget *widget = NULL;
    GdkPixmap *background = NULL;
    GtkStyle *style = NULL;
 
    gtk_init (&argc, &argv);
    /* Load a non animated gif */
    image = load_pixbuf_from_file ("/home/midnigther/Desktop/pict.gif");
    //  widget = gtk_image_new_from_pixbuf (image);
    gdk_pixbuf_render_pixmap_and_mask (image, &background, NULL, 0);
    style = gtk_style_new ();
    style->bg_pixmap [0] = background;
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW(window), "Load Image");
    gtk_window_set_default_size (GTK_WINDOW (window), 400, 300);
    gtk_widget_set_style (GTK_WIDGET(window), GTK_STYLE (style));
    gtk_window_set_transient_for (GTK_WINDOW (window), NULL);
 
    GtkWidget *hbox = NULL;
    hbox = gtk_hbox_new (0, FALSE);
    gtk_container_add (GTK_CONTAINER(window), hbox);
 
    GtkWidget *button = NULL;
    button = gtk_button_new_with_label ("Sonic");
    gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
 
    gtk_widget_show_all (window);
    gtk_main ();
    return 0;
}

参考: http://stackoverflow.com/questions/10823256/how-does-one-set-the-background-of-a-gtkwindow-to-an-image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值