我使用下面的代码来设置背景图像,但由于图像的大小很小,我想拉伸大小以适合屏幕,或者如果图像比屏幕大,在这种情况下,我也需要相同的
使用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;
}
我不认为你真的需要布局设置背景图像。你可以重载"绘制"信号,在处理程序中你可以绘制背景图像。下面的代码将为您工作。
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参数来拉伸图像。