gpt4 book ai didi

c - 如何在 GTK+ 中启动和停止 GIF 动画 GtkImage

转载 作者:太空狗 更新时间:2023-10-29 15:33:24 26 4
gpt4 key购买 nike

我有一个用 C 编写的 GTK+ 应用程序,它加载一个动画 GIF 文件矩阵。这些 GtkImages 在加载时自动运行动画,然后在动画完成时停止。我将如何重新启动包含 GIF 的每个 GtkImage 的动画以及动画完成时是否生成信号?

谢谢。

编辑:
gdk_pixbuf_animation_get_iter() 的使用是否描述了here让这成为可能?

下面提供了完整的代码。

/*
* Compile me with:
* gcc -o reels reels.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0)
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>

/* GTK */
#include <gdk/gdkx.h>
#include <gtk/gtk.h>

/**** prototypes ****/

static void destroy (GtkWidget*, gpointer);
GdkPixbuf *create_pixbuf(const gchar * filename);
GtkWidget *SetupWindow(gchar *data, const gchar *filename);
static void destroy (GtkWidget *window, gpointer data);
void btnSpin_clicked(GtkWidget *button, gpointer data);
void btnExit_clicked(GtkWidget *button, gpointer data);

/********************/

GtkWidget *images[3][5];

static void destroy (GtkWidget *window, gpointer data)
{
gtk_main_quit ();
}

void btnSpin_clicked(GtkWidget *button, gpointer data)
{
printf("Spin Button pressed.\n");

return;
}

void btnExit_clicked(GtkWidget *button, gpointer data)
{
gtk_main_quit();

return;
}


GtkWidget *SetupWindow(gchar *data, const gchar *filename)
{
GdkPixmap *background;
GdkPixbuf *pixbuf;
GdkScreen *ourscreen;
GdkColormap *colormap;
GtkStyle *style;
GdkColor fg;
GdkColor bg;
GError *error = NULL;
GdkRectangle *rect;
GtkWidget *window;

pixbuf = gdk_pixbuf_new_from_file (filename,&error);
if (error != NULL) {
if (error->domain == GDK_PIXBUF_ERROR) {
g_print ("Pixbuf Related Error:\n");
}
if (error->domain == G_FILE_ERROR) {
g_print ("File Error: Check file permissions and state:\n");
}

g_printerr ("%s\n", error[0].message);
}

gdk_pixbuf_render_pixmap_and_mask (pixbuf, &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), data);
// gtk_window_maximize(GTK_WINDOW(window));
gtk_window_set_modal (GTK_WINDOW (window),TRUE);
gtk_window_set_default_size(GTK_WINDOW(window),628,530);
gtk_widget_set_style (GTK_WIDGET(window), GTK_STYLE(style));
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER_ALWAYS);
gtk_container_set_border_width(GTK_CONTAINER(window), 0);
//gtk_window_set_resizable(GTK_WINDOW(window), (gboolean) FALSE);
gtk_window_set_decorated( GTK_WINDOW(window), FALSE );

return(window);
}

int main (int argc, char *argv[])
{
GdkPixbufAnimation *animation;
GtkWidget *image;
int x,y;
GdkPixbuf *pixBuf;
GdkPixmap *pixMap;
gchar filename[20];
GtkWidget *btnSpin, *btnExit;

GtkWidget *frame; /* for absolute positionining of widgets */
GtkWidget *window;
int posx, posy;

gtk_init (&argc, &argv);

window = SetupWindow("Demo", "background.gif");
g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy), NULL);

frame = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), frame);

btnSpin = gtk_button_new_with_label("Spin");
gtk_widget_set_size_request(btnSpin, 80, 35);
gtk_fixed_put(GTK_FIXED(frame), btnSpin, 229, 485);
g_signal_connect(G_OBJECT( btnSpin ), "clicked", G_CALLBACK(btnSpin_clicked), NULL );

btnExit = gtk_button_new_with_label("Exit");
gtk_widget_set_size_request(btnExit, 80, 35);
gtk_fixed_put(GTK_FIXED(frame), btnExit, 320, 485);
g_signal_connect(G_OBJECT( btnExit ), "clicked", G_CALLBACK(btnExit_clicked), NULL );

/* setup animated gifs */
for( y = 0; y < 3; y++ )
{
posy = (y*120) + 20;
for( x = 0; x < 5; x++ )
{
posx = (x*120) + 20;
/* set each Image widget to spin GIF */
sprintf( filename,"%d-%d.gif", y+1,x+1 );
images[y][x] = gtk_image_new_from_file(filename);
gtk_fixed_put(GTK_FIXED(frame), images[y][x], posx, posy);
}
}

gtk_widget_show_all(window);

gtk_main ();

return 0;
}

最佳答案

查看 GtkImage source code 后,恐怕没有动画完成时产生的信号。但是,您应该能够通过调用 gtk_image_set_from_file() 来重新启动动画。或 gtk_image_set_from_animation() .

为了完全解决您最初的问题,我建议您创建一个 GtkImage 的子类。它的行为应该与 GtkImage 完全一样,除了 animation_timeout。如果 delay < 0 应该发送一个信号周围line 1315 .

关于创建 GObject 子类的信息(注意 GtkImage 是一个 GObject)可以在 here 中找到。 .

关于c - 如何在 GTK+ 中启动和停止 GIF 动画 GtkImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10840069/

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