http://caterpillar.onlyfun.net/Gossip/GTKGossip/GtkFileChooserButtonFileChooserDialog.html
還另外參考了GTK+ Reference Manual(沒有它不行啊!) http://library.gnome.org/devel/gtk/stable/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
// test_GtkFileChooserDialog.c // GTK圖片顯示之測試小程式,按下按鈕可打開目錄選取圖片, // 選取後會自動將圖片顯示出來,並將完整目錄註記於標題上。 // update : 2010.5.7 #include <stdlib.h> #include <gtk/gtk.h> void file_changed_dir(GtkFileChooserButton *button, GtkWidget *window) // 將選取圖片的完整位址顯示在標題上 { gchar *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(button)); gtk_window_set_title(GTK_WINDOW(window), file); } void file_changed_image(GtkFileChooserButton *button, GtkWidget *box) // 將選取的圖片顯示在畫面(box)上 { static GtkWidget *image; // 宣告成static,可保存圖片資料到下次使用 gchar *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(button)); gtk_container_remove(GTK_CONTAINER(box), image); // 把先前的圖片拿掉 image = gtk_image_new_from_file(file); gtk_container_add(GTK_CONTAINER(box), image); gtk_widget_show(image); } GtkFileFilter *build_gtk_file_filer( char *showStyle ) // 設定目錄顯示方式,目前預設有兩種:1只顯示圖檔 2.顯示全部檔案 { GtkFileFilter *filter = gtk_file_filter_new(); if ( strcmp( showStyle, "Image Files" ) == 0 ) { // 預設為圖片,只顯示PNG、GIF、JPG和ICO圖檔 gtk_file_filter_set_name(filter, "Image Files"); gtk_file_filter_add_pattern(filter, "*.png"); gtk_file_filter_add_pattern(filter, "*.gif"); gtk_file_filter_add_pattern(filter, "*.jpg"); gtk_file_filter_add_pattern(filter, "*.ico"); } else { // 任何檔案皆符合 gtk_file_filter_set_name(filter, "All Files"); gtk_file_filter_add_pattern(filter, "*"); } return filter ; } int main(int argc, char *argv[]) { GtkWidget *window; GtkWidget *label; GtkWidget *button; GtkFileFilter *filter1, *filter2; GtkWidget *vbox ; GtkWidget *image; gchar *filename = "origin.gif"; // 預設圖檔 gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "GtkFileChooserButton"); gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER); button = gtk_file_chooser_button_new( "選取檔案", GTK_FILE_CHOOSER_ACTION_OPEN); // 呼叫選取目錄 filter1 = build_gtk_file_filer( "Image Files" ) ; filter2 = build_gtk_file_filer( "All Files" ) ; // 設定filter順序 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(button), filter1); gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(button), filter2); label = gtk_label_new("show PIC"); vbox = gtk_vbox_new(FALSE, 5); // 設定為FALSE,各區的大小才不會平均分配。 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 5); gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 5); gtk_container_add(GTK_CONTAINER(window), vbox); g_signal_connect(GTK_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL); g_signal_connect(GTK_OBJECT(button), "selection_changed", G_CALLBACK(file_changed_dir), window); // 處理目錄顯示 g_signal_connect(GTK_OBJECT(button), "selection_changed", G_CALLBACK(file_changed_image), vbox); // 處理圖片顯示 gtk_widget_show_all(window); gtk_main(); return 0; }
No response to “GTK顯示圖片的測試小程式” ;
張貼留言