Linux GTK编程示例
1 #include <gtk/gtk.h> 2 3 static void helloWorld(GtkWidget *wid, GtkWidget *win) 4 { 5 GtkWidget *dialog = NULL; 6 dialog = gtk_message_dialog_new(GTK_WINDOW(win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Hello World!"); 7 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER); 8 gtk_dialog_run(GTK_DIALOG(dialog)); 9 gtk_widget_destroy(dialog); 10 } 11 12 int main(int argc, char *argv[]) 13 { 14 GtkWidget *button = NULL; 15 GtkWidget *win = NULL; 16 GtkWidget *vbox = NULL; 17 18 // g_log_set_handler("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL); 19 gtk_init (&argc, &argv); 20 // g_log_set_handler("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL); 21 22 win = gtk_window_new(GTK_WINDOW_TOPLEVEL); 23 gtk_container_set_border_width(GTK_CONTAINER(win), 8); 24 gtk_window_set_title(GTK_WINDOW(win), "Hello World"); 25 gtk_window_set_position(GTK_WINDOW (win), GTK_WIN_POS_CENTER); 26 gtk_widget_realize(win); 27 g_signal_connect(win, "destroy", gtk_main_quit, NULL); 28 29 /* Create a vertical box with buttons */ 30 vbox = gtk_box_new(TRUE, 6); 31 gtk_container_add(GTK_CONTAINER(win), vbox); 32 33 button = gtk_button_new_with_label("hello"); 34 g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(helloWorld), (gpointer)win); 35 gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 0); 36 button = gtk_button_new_with_label("exit"); 37 g_signal_connect(button, "clicked", gtk_main_quit, NULL); 38 gtk_box_pack_start(GTK_BOX (vbox), button, TRUE, TRUE, 0); 39 40 gtk_widget_show_all(win); 41 gtk_main(); 42 return 0; 43 }
1 all: 2 @#gcc -Wall -g -o app main.c `pkg-config --cflags --libs gtk+-3.0` 3 @gcc -Wall -g -o app main.c -pthread -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/cairo -I/usr/include/pango-1.0 -I/usr/include/harfbuzz -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng16 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 4 5 run: 6 ./app 7 8 clean: 9 @rm -rf ./app