gpt4 book ai didi

c++ - 如何在 GTK+ 中拉伸(stretch)图像?

转载 作者:行者123 更新时间:2023-11-28 06:15:46 25 4
gpt4 key购买 nike

我正在使用以下代码来设置背景图像,但由于图像尺寸较小,我想拉伸(stretch)尺寸以适合屏幕,或者如果图像大于屏幕,在这种情况下我也需要相同的尺寸。

我正在使用 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);

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

关于c++ - 如何在 GTK+ 中拉伸(stretch)图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30367292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com