|
自己参照API写了一个实现动画的小程序,拿来共享,请大虾指点
本程序没有涉及错误检查、释放内存。
顺便请教g_object_ref()和g_object_unref()在什麽情况下要使用?
[code:1]
#include <gtk/gtk.h>
GtkWidget *window;
GtkWidget *eventbox;
GdkPixbufAnimationIter *iter;
GtkWidget *image;
GdkPixbuf *buf;
/*主要代码*/
void draw()
{
gtk_container_remove(GTK_CONTAINER(eventbox),image);
gdk_pixbuf_animation_iter_advance(iter,NULL);
buf = gdk_pixbuf_animation_iter_get_pixbuf(iter);
image = gtk_image_new_from_pixbuf(buf);
gtk_container_add(GTK_CONTAINER(eventbox),image);
gtk_widget_show(image);
}
void quit(GtkWidget *widget,GdkEventButton *button,gpointer data)
{
if (button->button == 1)
gtk_main_quit();
}
int main(int argc,char *argv[])
{
GdkPixbufAnimation *animation;
guint time;
int width,height;
gtk_init(&argc,&argv);
animation = gdk_pixbuf_animation_new_from_file("gif006.gif",NULL);
width = gdk_pixbuf_animation_get_width(animation);
height = gdk_pixbuf_animation_get_height(animation);
window = gtk_window_new(GTK_WINDOW_POPUP);
gtk_window_set_title(GTK_WINDOW(window),"Animation Window");
gtk_window_set_default_size(GTK_WINDOW(window),width,height);
gtk_window_set_position(GTK_WINDOW(window),
GTK_WIN_POS_CENTER);
eventbox = gtk_event_box_new();
g_signal_connect(G_OBJECT(eventbox),"button_press_event",
G_CALLBACK(quit),NULL);
gtk_container_add(GTK_CONTAINER(window),eventbox);
gtk_widget_show(eventbox);
/*主要代码*/
iter = gdk_pixbuf_animation_get_iter(animation,NULL);
buf = gdk_pixbuf_animation_iter_get_pixbuf(iter);
time = gdk_pixbuf_animation_iter_get_delay_time(iter);
image = gtk_image_new_from_pixbuf(buf);
gtk_container_add(GTK_CONTAINER(eventbox),image);
gtk_widget_show(image);
gtk_widget_show(window);
/*根据动画自身的时间间隔来设置draw()函数*/
gtk_timeout_add(time,(GtkFunction)draw,iter);
gtk_main();
return 0;
}
[/code:1] |
|