poseidon_win 发表于 2011-6-2 18:41:55

写得很成功。但是我的显示不了文件——字体有问题

librehat 发表于 2011-7-25 23:48:45

在楼主的基础上,将关闭按钮改为校验按钮,同时增加一个entry用于输入进行文件匹配比对的MD5值,同时将计算的结果直接反馈到entry。
效果如图:

代码如下:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <sys/stat.h>
#include <ctype.h>

GtkWidget *choose_button = NULL;
static gchar enter;
static gchar calc_r;

static void calc_Md5 (GtkWidget *wid, GtkWidget *entry)
{
GtkWidget *dialog = NULL;
gchar *filename = NULL;
const gchar *result = NULL;
gchar mess;
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(choose_button));
if (filename){
    GChecksum * checksum;
    guchar * buffer = NULL;
    FILE *fp = NULL;

    if ((fp = fopen(filename, "rb"))!=NULL){
      /* get file size */
      struct stat filesize;
      stat(filename, &filesize);
      int file_no = filesize.st_size;
      if (file_no != -1){
            /* read buffer */
            buffer = (guchar *)g_malloc(sizeof(guchar)*file_no);
            fread(buffer, file_no, 1, fp);

            /* checksum */
            checksum = g_checksum_new(G_CHECKSUM_MD5);
            g_checksum_update(checksum, (const guchar *)buffer, file_no);
            result = g_checksum_get_string(checksum);
            g_free(buffer);
      }else{
            strcpy ((gchar *)result, "文件打开失败!");
      }
      fclose(fp);
    }else{
      strcpy ((gchar *)result, "文件打开失败!");
    }
    strcpy(mess, result);
    g_free((gchar *)result);
    g_free(filename);
}else{
    strcpy(mess, "请选择或者拖动一个文件!");
}
if(entry)    gtk_entry_set_text(GTK_ENTRY(entry), mess);
strcpy(calc_r,mess);
}

void enter_callback(GtkWidget *wid, GtkWidget *entry)
{
    const gchar *tenter;
    tenter = gtk_entry_get_text (GTK_ENTRY(entry));
    strcpy(enter,tenter);
    for ( gint i =0; i < 32; i++){
      if (isupper(enter))
            enter = tolower (enter);
    }
}

static void cmp_Md5 (GtkWidget *wid, GtkWindow *win)
{
GtkWidget *dialog;
gchar cmp_result;
if(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(choose_button)))
{
    calc_Md5 (wid, NULL);
    if((strcmp(calc_r, enter)) == 0)    strcpy(cmp_result, "文件匹配!");
    else    {strcpy(cmp_result, "文件不匹配!\\n");
      strcat(cmp_result, "计算值:");
      strcat(cmp_result, calc_r);
      strcat(cmp_result, "\\n输入值:");
      strcat(cmp_result, enter);
    }
}
else    strcpy(cmp_result, "请先选择一个文件!");
dialog = gtk_message_dialog_new(win,
            GTK_DIALOG_MODAL,
            GTK_MESSAGE_INFO,
            GTK_BUTTONS_CLOSE,
            cmp_result);
gtk_window_set_title(GTK_WINDOW(dialog), "校验结果");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}

int main (int argc, char *argv[])
{
GtkWidget *button = NULL;
GtkWidget *win = NULL;
GtkWidget *vbox = NULL;
GtkWidget *hbox = NULL;
GtkWidget *label = NULL;
GtkWidget *entry = NULL;

/* Initialize GTK+ */
g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
gtk_init (&argc, &argv);
g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);

/* Create the main window */
win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width (GTK_CONTAINER (win), 8);
gtk_window_set_title (GTK_WINDOW (win), "MD5 计算");
gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
gtk_widget_realize (win);
gtk_widget_set_size_request(win, 300,150);
g_signal_connect (win, "destroy", gtk_main_quit, NULL);

/* Create a vertical box with buttons */
vbox = gtk_vbox_new (TRUE, 6);
gtk_container_add (GTK_CONTAINER (win), vbox);

hbox = gtk_hbox_new (FALSE, 6);
gtk_box_pack_start(GTK_BOX (vbox), hbox, TRUE, TRUE, 0);

label = gtk_label_new("文件:");
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);

choose_button = gtk_file_chooser_button_new ("选择一个文件",
                                        GTK_FILE_CHOOSER_ACTION_OPEN);
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (choose_button),
                                       "$HOME");
gtk_box_pack_start (GTK_BOX (hbox), choose_button, TRUE, TRUE, 0);

entry = gtk_entry_new ();
gtk_entry_set_max_length(GTK_ENTRY (entry), 32);
gtk_entry_set_text (GTK_ENTRY (entry), "在这里输入用于校验文件的MD5值");
gtk_box_pack_start(GTK_BOX (vbox), entry, TRUE, TRUE, 0);

hbox = gtk_hbox_new (TRUE, 6);
gtk_box_pack_start(GTK_BOX (vbox), hbox, TRUE, TRUE, 0);

button = gtk_button_new_with_label("计算");
gtk_box_pack_start(GTK_BOX (hbox), button, TRUE, TRUE, 0);
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (calc_Md5), G_OBJECT (entry));


button = gtk_button_new_with_label("校验");
g_signal_connect (button, "clicked", G_CALLBACK(enter_callback), entry);
g_signal_connect_after (button, "clicked", G_CALLBACK(cmp_Md5), (gpointer)win);
gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);

/* Enter the main loop */
gtk_widget_show_all (win);
gtk_main ();
return 0;
}

编译时,可能需要加上-std=c99指令

yong230 发表于 2011-9-3 22:47:44

请问一下,你那个程序用的什么主题,很漂亮啊
页: 1 [2]
查看完整版本: GTK+写的MD5计算工具