Gtk是图形界面库,Glade是Gtk界面的快速开发工具。可以使用Glade_with_Gtk+一体化安装包。地址:
http://ftp.gnome.org/pub/GNOME/binaries/win32/glade3/3.6/
本文将Gtk+安装在D盘根目录下。安装正确后在Windows的cmd窗口中分别运行pkg-config --cflags gtk+-2.0和pkg-config --libs gtk+-2.0后会有如下显示:
(1)>pkg-config --cflags gtk+-2.0-mms-bitfields -Id:/Gtk+/include/gtk-2.0 -Id:/Gtk+/lib/gtk-2.0/include -Id:/Gtk+/include/atk-1.0
-Id:/Gtk+/include/cairo -Id:/Gtk+/include/pango-1.0 -Id:/Gtk+/include/glib-2.0 -Id:/Gtk+/lib/glib-2.0/include
-Id:/Gtk+/include/libpng12
(2)>pkg-config --libs gtk+-2.0
-Ld:/Gtk+/lib -lgtk-win32-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio-2.0 -lgdk_pixbuf-2.0 -lpangowin32-1.0 -lgdi32
-lpangocairo-1.0 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -lglib-2.0 -lintl
三.创建工程
新建project new->c project,project type 选择Hello world ANSI c project,然后按如下步骤配置:
在project->properties->c/c++ build 下 setting :
(1)gcc complier->dirctories->include paths (-I) 中一项项添加命令的结果‘pkg-config --cflags gtk+-2.0’,即添加头文件路径
即一项项添加
d:/Gtk+/include/gtk-2.0
d:/Gtk+/lib/gtk-2.0/include
d:/Gtk+/include/atk-1.0
d:/Gtk+/include/cairo
d:/Gtk+/include/pango-1.0
d:/Gtk+/include/glib-2.0
d:/Gtk+/lib/glib-2.0/include
d:/Gtk+/include/libpng12
(2)对于linker:
GCC C linker -> libraries中添加命令'pkg-config --libs gtk+-2.0'执行结果
即一项项添加
gtk-win32-2.0
gdk-win32-2.0
atk-1.0
gio-2.0
gdk_pixbuf-2.0
pangowin32-1.0
gdi32
pangocairo-1.0
pango-1.0
cairo
gobject-2.0
gmodule-2.0
glib-2.0
intl
另外还需加入库的搜索路径d:/Gtk+/lib
三.GTK程序Hello_world示例
#include<gtk/gtk.h>
/*定义回调函数hello,点击按钮时被系统自动调用*/
void hello(GtkWidget *widget,gpointer *data)
{
g_print("button clicked and data = %s\n",(char *)data);
}
/*定义回调函数destroy,关闭窗口时被系统自动调用*/
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);
g_signal_connect(GTK_OBJECT(window),"destroy",
GTK_SIGNAL_FUNC(destroy),NULL);
/*设置窗口的属性*/
gtk_container_border_width(GTK_CONTAINER(window),20);
/*创建按钮,并设置当点击按钮时,要执行的回调函数*/
button = gtk_button_new_with_label("Hello World");
g_signal_connect(GTK_OBJECT(button),"clicked",
GTK_SIGNAL_FUNC(hello),"I am from button");
/*将按钮加入到窗口中*/
gtk_container_add(GTK_CONTAINER(window),button);
/*显示按钮和窗口*/
gtk_widget_show(button);
gtk_widget_show(window);
/*进入消息处理循环*/
gtk_main();
return 0;
}
编辑运行就可以了