#include <gtk/gtk.h> typedef struct _ProgressData { GtkWidget *window; GtkWidget *pbar; GtkWidget *vbox; GtkWidget *image; GtkWidget *align; int timer; }ProgressData; typedef struct _MainData { GtkWidget *MainWin; }MainData; MainData *pmdata;
static void destroy_progress( GtkWidget *widget, ProgressData *pdata); /* 更新进度条,这样就能够看到进度条的移动 */ static gint progress_timeout( gpointer data ) { ProgressData *pdata = (ProgressData *)data; gdouble new_val; /* 使用在调整对象中设置的取值范围计算进度条的值 */ new_val = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR(pdata->pbar)) + 0.05; if (new_val > 1.0) { //new_val = 0.0; /* 当进度条100%之后执行其他的操作 */ gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pdata->pbar),1.0); g_usleep(500); destroy_progress(pdata->pbar,pdata); return FALSE; } /* 设置进度条的新值 */ gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (pdata->pbar),new_val); /* 这是一个timeout函数,返回 TRUE,这样它就能够继续被调用 */int main( int argc, char *argv[]) { ProgressData *pdata = NULL; gtk_init (&argc, &argv); /* 为传递到回调函数中的数据分配内存 */ pdata = g_malloc (sizeof (ProgressData)); pmdata = g_malloc (sizeof (MainData)); pdata->window = gtk_window_new (GTK_WINDOW_POPUP); gtk_window_set_resizable (GTK_WINDOW (pdata->window), FALSE); /* 不连接gtk_main_quit回调,这样就可以安全的destroy掉splash窗口了 */ //g_signal_connect (G_OBJECT (pdata->window), "destroy",G_CALLBACK (gtk_main_quit),NULL); gtk_window_set_position(GTK_WINDOW(pdata->window), GTK_WIN_POS_CENTER_ALWAYS); gtk_widget_set_size_request(pdata->window, 500, 350); /* main window will display after splash window */ pmdata->MainWin = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_resizable (GTK_WINDOW(pmdata->MainWin), FALSE); g_signal_connect (G_OBJECT (pmdata->MainWin), "destroy",G_CALLBACK (gtk_main_quit),NULL); gtk_window_set_position(GTK_WINDOW(pmdata->MainWin), GTK_WIN_POS_CENTER_ALWAYS); gtk_widget_set_size_request(pmdata->MainWin, 500, 500); /* vbox for layout splash window */ pdata->vbox = gtk_vbox_new (FALSE, 5); gtk_container_set_border_width (GTK_CONTAINER (pdata->vbox), 0); gtk_container_add (GTK_CONTAINER (pdata->window), pdata->vbox); gtk_widget_show (pdata->vbox); /* image */ pdata->image = gtk_image_new_from_file("image/splash.png"); gtk_box_pack_start (GTK_BOX (pdata->vbox), pdata->image, TRUE, TRUE, 0); gtk_widget_show(pdata->image); /* 创建一个居中对齐的对象 */ pdata->align = gtk_alignment_new (0.5, 1, 1, 1); gtk_box_pack_start (GTK_BOX (pdata->vbox), pdata->align, FALSE, FALSE, 0); gtk_widget_show (pdata->align); /* 创建进度条 */ pdata->pbar = gtk_progress_bar_new (); gtk_container_add (GTK_CONTAINER (pdata->align), pdata->pbar); gtk_widget_show (pdata->pbar); /* 加一个定时器(timer),以更新进度条的值 */ pdata->timer = gtk_timeout_add (100, progress_timeout, pdata); gtk_widget_show (pdata->window); //gtk_widget_show(pdata->MainWin); gtk_main (); return 0; }
return TRUE;}/* 清除分配的内存,删除定时器(timer), 进度条走满后调用的函数,可以用来做很多事情 */static void destroy_progress( GtkWidget *widget, ProgressData *pdata){ gtk_timeout_remove (pdata->timer); pdata->timer = 0; gtk_widget_destroy(pdata->pbar); gtk_widget_destroy(pdata->image); //gtk_widget_hide(pdata->window); gtk_widget_destroy(pdata->window); g_free(pdata); gtk_widget_show(pmdata->MainWin);}