1、使用glade设计Gtk+界面,保存为gtkbuilder文件,比如:

 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
<?xml version="1.0"?>
<!--Generated with glade3 3.4.5 on Sun Nov 29 12:39:11 2009 -->
<interface>
  <object class="GtkWindow" id="window1">
    <child>
      <object class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <child>
          <object class="GtkImage" id="image1">
            <property name="visible">True</property>
            <property name="pixbuf">stallman.jpg</property>
          </object>
        </child>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="label" translatable="yes">Richard Stallman</property>
          </object>
          <packing>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

2、编写程序,调用GtkBuilder

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <gtk/gtk.h>

static void window_close(GtkWidget *widget,gpointer data)
{
  gtk_main_quit();
}

int main(int argc,char *argv[])
{
  GtkBuilder *builder = NULL;
  GError *error = NULL;      //注意要初始化变量!!!
  GtkWidget *window = NULL;
  gtk_init(&argc,&argv);
  builder=gtk_builder_new();
  gtk_builder_add_from_file(builder,"fsf.ui",&error);
 window=GTK_WIDGET(gtk_builder_get_object(builder,"window1"));
  g_object_unref(G_OBJECT(builder));
 g_signal_connect(window,"destroy",G_CALLBACK(window_close),NULL);
  gtk_widget_show_all(window);
  gtk_main();
  return 0;
}

3、用cb编译运行,OK!