如何在GTK +中拉伸图像?

时间:2015-05-21 07:31:27

标签: c++ user-interface gtk

我正在使用以下代码来设置背景图像,但由于图像尺寸较小,我想拉伸尺寸以适合屏幕,或者如果图像大于屏幕,那么我也需要相同的颜色。< / p>

我正在使用Gtk + 3.2

#include <gtk/gtk.h>

int main( int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *layout;
GtkWidget *image;
GtkWidget *button;

gtk_init(&argc, &argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 290, 200);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

layout = gtk_layout_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER (window), layout);
gtk_widget_show(layout);

image = gtk_image_new_from_file("/home/my_background_image.jpg");
gtk_layout_put(GTK_LAYOUT(layout), image, 0, 0);

button = gtk_button_new_with_label("Button");
gtk_layout_put(GTK_LAYOUT(layout), button, 150, 50);
gtk_widget_set_size_request(button, 80, 35);

g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);

gtk_widget_show_all(window);

gtk_main();

return 0;
}

1 个答案:

答案 0 :(得分:1)

我认为你真的不需要布局来设置背景图像。 你可以超载&#34;画&#34;信号和处理程序,你可以绘制背景图像。 以下代码适合您。

gboolean GtkDrawing::window_draw_cb (GtkWidget * widget, cairo_t * cr, cairo_surface_t* m_bgImage)
{
    gint root_width,root_height;

        cairo_set_source_surface (cr,m_bgImage, 0, 0);

        gtk_window_get_size (GTK_WINDOW(widget), &root_width, &root_height);
        cairo_rectangle (cr, 0, 0,root_width, root_height);
        cairo_fill (cr);

        //Enable Below code to draw child widget after background rendering
        //gtk_widget_draw (childwidget, cr);
        //cairo_fill (cr);


    return TRUE;
}

cairo_surface_t* m_bgImage=cairo_image_surface_create_from_png("/home/my_background_image.png");
g_signal_connect (G_OBJECT (window), "draw", G_CALLBACK(GtkDrawing::window_draw_cb), m_bgImage);

在上面的处理函数中,要拉伸图像,可以修改cairo_rectangle参数。