OS:Ubuntu 8.04 Hardy Heron
IDE: Code::Blocks svn build rev 5020 with Gtk+-2.0
在 /etc/apt/sources.list 中加入以下網址:IDE: Code::Blocks svn build rev 5020 with Gtk+-2.0
# Code::Blocks
deb http://apt.wxwidgets.org/ gutsy-wx main
deb http://archive.ubuntu.com/ubuntu/ gutsy multiverse restricted universe main
deb http://lgp203.free.fr/ubuntu/ gutsy universe
linly-desktop:$ wget -q http://apt.wxwidgets.org/key.asc -O- | sudo apt-key add - linly-desktop:$ wget -q http://lgp203.free.fr/public.key -O- | sudo apt-key add - linly-desktop:$ sudo apt-get update linly-desktop:$ sudo apt-get install libgtk2.0-dev linly-desktop:$ sudo apt-get install codeblocks準備工作完成後,接著開始我的第一支 Gtk+ 程式...
首先開啟 Code::Blocks,選擇一個 console application,接著在以下兩處作設定:
- Code::Blocks 的 Setting->Compiler and debugger->Compiler settings->Other options 加入`pkg-config --cflags gtk+-2.0`。
- Code::Blocks 的 Setting->Compiler and debugger->Linker settings->Other linker options 加入`pkg-config --libs gtk+-2.0`。
#include<gtk/gtk.h> gint delete_event(GtkWidget *widget, GdkEvent *event, gpointer data) { g_print ("delete event occurred\n"); return(TRUE); } void destroy(GtkWidget *widget, gpointer data) { gtk_main_quit(); } int main(int argc, char *argv[]) { GtkWidget *window; GtkWidget *button; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); // connect signal gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(delete_event), NULL); gtk_signal_connect(GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC (destroy), NULL); gtk_container_set_border_width(GTK_CONTAINER (window), 10); button = gtk_button_new_with_label("Hello Ubuntu!"); gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(window)); // 把 button 加入 window 中 gtk_container_add(GTK_CONTAINER (window), button); gtk_widget_show(button); gtk_widget_show(window); gtk_main(); return(0); }
2 則留言:
GTK_SIGNAL_FUNC這個巨集是將事件(or訊號)處理函式轉型成GtkSignalFunc,而該函式指標的原型為 void (*GtkSignalFunc) (); so amazing!!
在自訂的delete_event中,回傳值為TRUE,就不會在後續去執行預設的處理函式,所以也不會發出destroy的信號。
張貼留言