Linux 下从头再走 GTK+-3.0 (一)

  原本由于项目需求在 Linux 下学习过一段时间的 GTK+2.0 图形开发,时隔一段时间,想真正深入学习一下 GTK 。

这次直接从头学习 GTK+-3.0 ,并写下博文便于日后查看,也方便新手入门。

  安装环境参考:

    Fedora:http://www.cnblogs.com/watsonlong/archive/2011/04/06/2006989.html

    Ubuntu:http://www.cnblogs.com/niocai/archive/2011/07/15/2107472.html

    Arch    :直接执行 sudo pacman -S gtk3 即可。

  注意采用的是GTK3,上文中大部分是GTK2,注意同样的方式安装GTK3。

 

 首先学习到的便是创建一个空白的窗口。我们创建一个名为 example.c 的源文件。内容如下

 #include <gtk/gtk.h>            //每个GTK程序都要包含的头文件,里面申明了很多类型,函数原型等

 static void activate (GtkApplication *app , gpointer data)
{
GtkWidget *window; //申明一个窗口 window = gtk_application_window_new(app); //为app创建一个窗口 gtk_window_set_title(GTK_WINDOW(window) , "Application"); //设置窗口的标题
gtk_window_set_default_size(GTK_WINDOW(window) , , ); //设置窗口默认大小为长宽各200像素
gtk_widget_show_all(window); //显示窗口
} int main(int argc , char **argv) //主函数
{
GtkApplication *app; //申明创建一个 GtkApplicatin对象名为app
int app_status; //用于拿到app 运行结束后的返回值 app = gtk_application_new("org.rain.gtk" , G_APPLICATION_FLAGS_NONE); //创建一个application
g_signal_connect(app , "activate" , G_CALLBACK(activate) , NULL); //连接信号,初始化app时,调用 activate 函数
app_status = g_application_run(G_APPLICATION(app) , argc , argv); //运行app g_object_unref(app); //销毁app return app_status;
}

  如下方式编译源文件,并执行生成的example可执行文件。

gcc example.c `pkg-config --cflags --libs gtk+-3.0` -Wall -o example
./example

  运行结果如下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAM8AAADuCAYAAACecVPMAAAABHNCSVQICAgIfAhkiAAAEdRJREFUeF7tnXlwlOUdx3+72VybgzNcSowp4QgmHK31AA+oFJVLVBzP0XacTmmnU2o7dsZjxqkH047ToR3r+AetVQdFRYrFY5BwCIJoPYCQiAmGk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gEJkAwhOZDWpAICoBhCcqHlSCQGQCCE9kNqgBgagETHhcURugEgRAIJRAZ2bQ84RywWsQ6CEBhKeHoNAMBEIJIDyhRPAaBHpIAOHpISg0A4FQAghPKBG8BoEeEvBUVu6zfwCEHyggAAKRCXBGqqr2n/0DIHl5edTQ0GD/qAEKCIBAZAKckdzcXMrIyMAfAImMCTUgEJsA5jyxGaEFCIQlgPCExYKNIBCbAMITmxFagEBYAghPWCzYCAKxCSA8sRmhBQiEJWDD43LhzuqwdLARBEIIOFnhZ7cp/A8ChMsEBGIQ6AqMzQsXt7MBvU8McqiOewKBWeGfPQkJCeTxeGya5s6ZTa2trfbR3t7uf3R0dMQ9OACIHwKBPQzngvMR+ODMcBtXdXV1x+nTp+29bYHPPp+PWlpa/EFidAhR/FxA8XimzujLCUxiYiIlJSXZ/8jK+Q+tnGfe7ikqKrIh4YcTGCc0bW1ttvfh0CA48Xg5xd852x6lczGAnFEZh8gJkvMz90SusqqjHb4WX2dwmpvtM7/moVtb1/AN4Ym/iyhezzgoPF3DtaTEJNsDJSYn22d+7Un00P8Bij85xQTdoAwAAAAASUVORK5CYII=" alt="" />

上一篇:面试题: nodejs 的事件轮询机制


下一篇:利用servlet产生随机数,原理是获取Graphics对象进行绘图