美文网首页
GTK<读取文件进度条>

GTK<读取文件进度条>

作者: 康大虎 | 来源:发表于2016-07-06 12:33 被阅读0次

    #include <gtk/gtk.h>

    #include <stdio.h>

    #include <string.h>

    #include <windows.h>

    #define FILE_READ_PATH "F:/123.txt"

    static float read_len = 0.0;

    gboolean update_progress_bar(gpointer data);

    int main(int argc,char *argv[])

    {

    GtkWidget *window,*vbox,*pbar;

    gtk_init(NULL,NULL);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);

    gtk_window_set_title(GTK_WINDOW(window),"Process bar");

    gtk_window_set_default_size(GTK_WINDOW(window),300,250);

    vbox = gtk_vbox_new(FALSE,0);

    gtk_container_add(GTK_CONTAINER(window),vbox);

    pbar = gtk_progress_bar_new();

    gtk_box_pack_start(GTK_BOX(vbox),pbar,FALSE,FALSE,50);

    g_timeout_add(100,update_progress_bar,pbar);

    g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL);

    gtk_widget_show_all(window);

    gtk_main();

    return 0;

    }

    gboolean update_progress_bar(gpointer data)

    {

    FILE *fp = NULL;

    //FILE *fwp = NULL;

    char buff[8] = {0};

    gint size = 10,file_len = 0;

    unsigned char *pbuff;

    GtkProgressBar *pbar = (GtkProgressBar *)data;

    if((fp = fopen(FILE_READ_PATH,"r")) == NULL)

    {

    return 0;

    }

    /*if((fwp = fopen("F:/kang.txt","ab")) == NULL)

    {

    return 0;

    }*/

    fseek(fp,0,SEEK_END);

    file_len = ftell(fp);

    pbuff = (unsigned char *)malloc((size+1)*sizeof(unsigned char));

    memset(pbuff,0,size*sizeof(unsigned char));

    if(read_len <= file_len)

    {

    fseek(fp,(long int)read_len,SEEK_SET);

    fread(pbuff,sizeof(unsigned char),size,fp);

    /* fwrite(pbuff,size*sizeof(unsigned char),1,fwp);*/

    gtk_progress_bar_set_fraction(pbar,read_len/file_len);

    sprintf(buff,"%d%s",(int)((read_len/file_len)*100),"%");

    gtk_progress_bar_set_text(GTK_PROGRESS_BAR(pbar),buff);

    read_len += size;

    return TRUE;   //必须返回TRUE;

    }

    if(read_len > file_len)

    {

    gtk_main_quit();

    return TRUE;

    }

    fclose(fp);

    /*fclose(fwp);*/

    free(pbuff);

    return TRUE;     //必须返回TRUE;

    }

    相关文章

      网友评论

          本文标题:GTK<读取文件进度条>

          本文链接:https://www.haomeiwen.com/subject/nnjkjttx.html