linux GTK教程(消息机制/标签/按钮/图像/文本/对话框/菜单/容器)

  GTK+(GIMP Toolkit)是一套源码以LGPL许可协议分发、跨平台的图形工具包。最初是为GIMP写的,已成为一个功能强大、设计灵活的一个通用图形库,是GNU/Linux下开发图形界面的应用程序的主流开发工具之一。

1.GTK安装

2.一个简单的GTK窗口程序

#include <stdio.h>
#include <gtk/gtk.h>

int main(int argc, char **argv)
{
    GtkWidget *window;
    gtk_init(&argc,&argv);
    
    window=gtk_window_new(GTK_WINDOW_TOPLEVEL);//参数表示创建主窗口
    
    g_signal_connect(G_OBJECT(window),"delete event",G_CALLBACK(gtk_main_quit),NULL);//空间和消息函数创建关联
    gtk_window_set_title(GTK_WINDOW(window),"test");
    gtk_widget_show(window);
    
    gtk_main();
    
    return 0;
}

g_signal_connect()函数原型:

gulong g_signal_connect(gpointer object, const gchar* name, GCallback func, gpointer func_data);
//object指向产生消息的控件
//name为消息名称
//func为发生消息后的回调函数
//func_data是床递给消息处理函数func的参数

//func
void callback_func(GtkWidget *widget, gpointer func_data);
//widget指向接收消息的控件
//func_data是传递的消息

//另外一个connect函数,它不提供消息的传送
gulong g_signal_connect_swapped(gpointer object,const gchar *name,GCallback func, gpointer slot_object);

3.为窗口增加一个按钮控件

//一般按钮的创建函数

GtkWidget* gtk_button_new(void)
//空按钮

GtkWidget* gtk_button_new_with_label(const gchar* label)
//带标签的

GtkWidget* gtk_button_new_with_mnemonic(congst gchar* label)
//带助记符的

GtkWidget* gtk_button_new_from_stock(const gchar* stock_id)
//基于固定項按钮--->由一个图标,一个文本标签和一个可选的快捷键组成

//其他函数

const gchar *gtk_button_get_label(GtkButton *button)
//获取标签文本

void gtk_button_set_label(GtkButton *button)

gboolean gtk_button_get_focus_on_click(GtkButton *button)
//获取按钮焦点
gboolean gtk_button_set_focus_on_click(GtkButton *button)
GtkWidget* gtk_button_get_image(GtkButton *button)
//获取按钮图像

GtkWidget* gtk_button_set_image(GtkButton *button)

  用户没有点击按钮,但是需要执行点击按钮时所对应的动作。一种方法是直接调用按钮的c l i c k e d信号的回调函数,另一种方法就是调用一个函数,让这个函数引发c l i c k e d信号,这样自然就会调用这个回调函数。对鼠标按下、松开、进入和离开等信号也有类似的函数。如下:

//对给定按钮构件b u t t o n引发p r e s s e d信号,效果是直接调用p r e s s e d信号对应的回调函数:
void gtk_button_pressed (GtkButton *button);
//对给定按钮构件b u t t o n引发r e l e a s e d信号:
void gtk_button_released (GtkButton *button);
//对给定按钮构件b u t t o n引发c l i c k e d信号:
void gtk_button_clicked (GtkButton *button);
//对给定按钮构件b u t t o n引发e n t e r信号:
void gtk_button_enter (GtkButton *button);
//对给定按钮构件b u t t o n引发l e a v e信号:
void gtk_button_leave (GtkButton *button);

例子:

View Code

4.标签创建和属性设置

GtkWidget *gtk_label_new(const char *str)
GtkWidget *gtk_laber_new_with_mnemonic(const char *str)
//第二个创建了一个带助记符的标签

void gtk_label_set_text(GtkLaber *label, const char *str)
//改变标签文本

const gchar* gtk_label_get_text(GtkLabel *label)
//获取标签当前文本

void gtk_label_set_justify(...)
//设置对齐方式

void gtk_label_set_line_wrap(..)
//激活自动换行

void gtk_label_set_pattern(..)
//加下划线

例子:

View Code
//laber_demo.c
#include <stdio.h>
#include <gtk/gtk.h>



int main(int argc, char **argv)
{
    GtkWidget *window;
    GtkWidget *label;     //标签
    GtkWidget *frame;     //框架
    GtkWidget *main_vbox; //组装盒
    
    gtk_init(&argc,&argv);
    
    window=gtk_window_new(GTK_WINDOW_TOPLEVEL);//参数表示创建主窗口
    g_signal_connect_swapped(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),G_OBJECT(window));
    
    gtk_window_set_title(GTK_WINDOW(window),"Label Demo");
    
    main_vbox=gtk_vbox_new(TRUE,6);
    gtk_container_set_border_width(GTK_CONTAINER(window),20);
    
    label=gtk_label_new("LABEL");
    gtk_box_pack_start(GTK_BOX(main_vbox),label,FALSE,FALSE,0);
    
    
    frame=gtk_frame_new("Normal label");
    label=gtk_label_new("this is a label");
    gtk_container_add(GTK_CONTAINER(frame),label);
    gtk_box_pack_start(GTK_BOX(main_vbox),frame,FALSE,FALSE,0);
    
    frame=gtk_frame_new("multi-line label");
    label=gtk_label_new("this is a multi-line label.\nsecond line.\nthird line.");
    gtk_container_add(GTK_CONTAINER(frame),label);
    gtk_box_pack_start(GTK_BOX(main_vbox),frame,FALSE,FALSE,0);
    
    frame=gtk_frame_new("line wrapped label");
    label=gtk_label_new("this is a line wrapped label."\
                        "second line."\
                        "third line.");
    gtk_label_set_line_wrap(GTK_LABEL(label),TRUE);
    gtk_container_add(GTK_CONTAINER(frame),label);
    gtk_box_pack_start(GTK_BOX(main_vbox),frame,FALSE,FALSE,0);
    
    gtk_container_add(GTK_CONTAINER(window),main_vbox);
    
    gtk_widget_show_all(window);
    
    gtk_main();
    
    return 0;
}

5.开关按钮,复选按钮,单选按钮

//创建开关按钮
GtkWidget *gtk_toggle_button_new(void)
GtkWidget *gtk_toggle_button_new_with_label(const gchar *label)
GtkWidget *gtk_toggle_button_new_with_mnemonic(const gchar *label)

//检查按钮状态
gboolean gtk_toggle_button_get_active(GtkToggleButton *toggle_button)

//设置按钮状态
void gtk_toggle_button_set_active(GtkToggleButton *toggle_button, gboolean is_active)
//创建复选按钮,检查函数同上
GtkWIdget *gtk_check_button_new(void)
GtkWIdget *gtk_check_button_new_with_label(const gchar* label)
GtkWIdget *gtk_check_button_new_with_mnemonic(const gchar* label)
//创建单选按钮
GtkWidget *gtk_radio_button_new( GSList *group );
GtkWidget *gtk_radio_button_new_from_widget( GtkRadioButton *group );
GtkWidget *gtk_radio_button_new_with_label( GSList *group,const gchar *label );
GtkWidget* gtk_radio_button_new_with_label_from_widget( GtkRadioButton *group,const gchar *label );
GtkWidget *gtk_radio_button_new_with_mnemonic( GSList *group,const gchar *label );
GtkWidget *gtk_radio_button_new_with_mnemonic_from_widget( GtkRadioButton *group,const gchar *label );

//单选按钮的属性

//创建组获取组标识
GSList *gtk_radio_button_get_group(GtkRadioButton *radio_button)

//设置组中的单选按钮
GSList *gtk_radio_button_set_group(GtkRadioButton *radio_button)

6.图像控制

//图像控件创建函数
GtkWidget* gtk_image_new(void)
GtkWidget* gtk_image_new_file(const gchar* filename)
GtkWidget* gtk_image_new_from_pixmap(GdkPixmap *pixmap)
GtkWidget* gtk_image_new_from_pixbuf(GdkPixbuf *pixbuf)
GtkWidget* gtk_image_new_from_stock(const gchar* stock_id)
GtkWidget* gtk_image_new_from_animation(GdkPixbufAnimation *animation)

//属性设置
void gtk_image_set_from_pixmap(...)
void gtk_image_set_from_image(...)
void gtk_image_set_from_file(...)
void gtk_image_set_from_pixfuf(...)
void gtk_image_set_from_stock(...)
void gtk_image_set_from_animation(...)

//图片信息获取函数
void gtk_image_get_pixmap(...)
void gtk_image_get_image(...)
GdkPixbuf* gtk_image_get_pixbuf(...)
void gtk_image_get_icon_name(...)
GdkPixbufAnimation* gtk_image_get_animation(...)

//清空图像
void gtk_image_clear(GtkImage *image)

例子:

View Code
#include <stdio.h>
#include <gtk/gtk.h>



int main(int argc, char **argv)
{
    GtkWidget *window;//主窗口
    GtkWidget *image;  //图像
    GtkWidget *vbox;  //组装盒
    
    gtk_init(&argc,&argv);
    
    window=gtk_window_new(GTK_WINDOW_TOPLEVEL);//参数表示创建主窗口
    g_signal_connect_swapped(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),G_OBJECT(window));
    
    gtk_window_set_title(GTK_WINDOW(window),"Image Demo");
    
    vbox=gtk_vbox_new(FALSE,0);
    gtk_container_add(GTK_CONTAINER(window),vbox);
    
    image=gtk_image_new_from_file("img.jpg");
    gtk_box_pack_start(GTK_BOX(vbox),image,TRUE,TRUE,0);
    
    image=gtk_image_new_from_stock(GTK_STOCK_HELP,GTK_ICON_SIZE_MENU);
    
    gtk_widget_show_all(window);
    
    gtk_main();
    
    return 0;
}

7.文本框输入

//单行输入
GtkWidget *gtk_entry_new(void)

//属性设置
void gtk_entry_set_max_length(GtkEntry* entry,gint max)
gint gtk_entry_get_max_length(GtkEntry* entry)
void gtk_entry_set_visibility(GtkEntry* entry,gboolean visible)
gboolean gtk_entry_get_visibility(GtkEntry* entry)
void gtk_editable_set_editable(GtkEditable* entry, gboolean editable)
gboolean gtk_editable_get_editable(GtkEditable* entry)
void gtk_entry_set_text(GtkEntry *entry, const gchar* text)
const gchar*  gtk_entry_get_text(GtkEntry *entry)


//多行输入
GtkWidget *gtk_text_new(GtkAdjustment *hadj, GtkAdjustment *vadj)

//属性设置
void gtk_text_set_editable(GtkText *text, gboolean editable)
void gtk_text_set_adjustments(GtkText *text,GtkAjustment* hadj,GtkAjustment *vadj)
void gtk_text_set_word_wrap(GtkText* text,gboolean word_wrap)
guint gtk_text_get_point(GtkText *text)
void gtk_text_set_point(GtkText *text,guint index)
guint gtk_text_get_length(GtkText *text)
void gtk_text_insert(GtkText *text,GdkFont *font,GdkColor *fore,GdkColor *back,const char *chars,gint length)

//创建滚动条
vscrollbar = gtk_vscrollbar_new(GTK_TEXT(text)->vadj)

例子:

View Code
//laber_demo.c
#include <stdio.h>
#include <gtk/gtk.h>

static void entry_toggle_visibility(GtkWidget *checkbutton,GtkWidget *entry)
{
    gtk_entry_set_visibility(GTK_ENTRY(entry),GTK_TOGGLE_BUTTON(checkbutton)->active);
}

void text_toggle_editable(GtkWidget *checkbutton,GtkWidget *text)
{
    gtk_text_set_editable(GTK_TEXT(text),GTK_TOGGLE_BUTTON(checkbutton)->active);
}

int main(int argc, char **argv)
{
    GtkWidget *window;//主窗口
    GtkWidget *vbox;  //组装盒
    GtkWidget *entry;
    GtkWidget *vscrollbar;
    GtkWidget *text;
    GtkWidget *table;
    GtkWidget *check;
    
    gtk_init(&argc,&argv);
    
    window=gtk_window_new(GTK_WINDOW_TOPLEVEL);//参数表示创建主窗口
    g_signal_connect_swapped(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),G_OBJECT(window));
    g_signal_connect_swapped(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_widget_destroy),G_OBJECT(window));
    gtk_window_set_title(GTK_WINDOW(window),"Entry and Text Demo");
    
    vbox=gtk_vbox_new(FALSE,0);
    gtk_container_add(GTK_CONTAINER(window),vbox);
    
    entry=gtk_entry_new();
    gtk_entry_set_max_length(GTK_ENTRY(entry),30);
    gtk_entry_set_text(GTK_ENTRY(entry),"this is a entry");
    gtk_box_pack_start(GTK_BOX(vbox),entry,TRUE,TRUE,0);
    
    check=gtk_check_button_new_with_label("Visible");
    gtk_box_pack_start(GTK_BOX(vbox),check,TRUE,TRUE,0);
    g_signal_connect(G_OBJECT(window),"toggled",G_CALLBACK(entry_toggle_visibility),(gpointer)entry);
    
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check),TRUE);
    
    table=gtk_table_new(2,2,FALSE);
    gtk_box_pack_start(GTK_BOX(vbox),table,TRUE,TRUE,0);
    
    text=gtk_text_new(NULL,NULL);
    gtk_text_set_editable(GTK_TEXT(text),TRUE);
    gtk_table_attach(GTK_TABLE(table),text,0,1,0,1,
                    GTK_EXPAND|GTK_SHRINK|GTK_FILL,
                    GTK_EXPAND|GTK_SHRINK|GTK_FILL,0,0);
    
    vscrollbar=gtk_vscrollbar_new(GTK_TEXT(text)->vadj);
    gtk_table_attach(GTK_TABLE(table),vscrollbar,1,2,0,1,
                    GTK_FILL,GTK_EXPAND|GTK_SHRINK|GTK_FILL,0,0);
    
    check=gtk_check_button_new_with_label("Editable");
    gtk_box_pack_start(GTK_BOX(vbox),check,FALSE,FALSE,0);
    g_signal_connect(G_OBJECT(check),"toggled",G_CALLBACK(text_toggle_editable),text);
    
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check),TRUE);
    
    gtk_widget_show_all(window);
    
    gtk_main();
    
    return 0;
}
//注意:编译时候,要添加-DGTK_ENABLE_BROKEN
gcc test.c -Wall -o test -DGTK_ENABLE_BROKEN `pkg-config --cflags --libs gtk+-2.0`

以上方法,可以解决下面的关于vscrollbar滚动条的错误:

error: invalid type argument of ‘->’ (have ‘int’)

8.对话框

//对话框结构
struct GtkDialog{
    GtkWindow window;           //窗口
    
    GtkWidget *vbox;            //垂直组装盒
    GtkWidget *action_area;     //活动区
    GtkWidget *separator;       //分割线
};


//创建函数
GtkWidget* gtk_dialog_new(void)
GtkWidget* gtk_dialog_new_with_buttons(const gchar  *title, GtkWindow *parent,
             GtkDialogFlags flags,
const gchar *first_button_text,...); //添加分割线 void gtk_dialog_set_has_separator(GtkDialog *dialog, gboolean setting) //添加按钮 GtkWidget* gtk_dialog_add_button(GtkDialog *dialog, const gchar *button_text,gint response_id); void gtk_dialog_add_buttons(GtkDialog *dialog,const gchar *first_button_text,...)

  为了开发程序方便,GTK在GtkDialog基础上,封装了一些常用的对话框,包括消息对话框(GtkMessageDialog),应用程序信息对话框(GtkAboutDialog),文本选择对话框(GtkFileChooserDialog),字体选择对话框(GtkFontSelectionDialog)等。

例子:

View Code
//laber_demo.c
#include <stdio.h>
#include <gtk/gtk.h>

void destroy(GtkWidget *widget,gpointer *data)
{
    gtk_widget_destroy(GTK_WIDGET(data));
}

void button_event(GtkWidget* widget,gpointer *data)
{
    GtkDialog *dialog;
    GtkWidget *button;
    dialog=gtk_dialog_new();
    gtk_window_set_title(GTK_WINDOW(dialog),"Normal Dialog");
    
    button=gtk_button_new_with_label("YES");
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),button,TRUE,TRUE,0);
    g_signal_connect(GTK_OBJECT(button),"clicked",G_CALLBACK(gtk_main_quit),NULL);
    
    gtk_widget_show(button);
    
    button=gtk_button_new_with_label("NO");
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),button,TRUE,TRUE,0);
    g_signal_connect(GTK_OBJECT(button),"clicked",G_CALLBACK(destroy),dialog);
    
    gtk_widget_show(button);
    
    gtk_widget_show(dialog);
}

void show_info_dialog(GtkWidget *widget,gpointer window)
{
    GtkDialog *dialog;
    dialog=gtk_message_dialog_new(window,GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_INFO,GTK_BUTTONS_OK,"infor to show","title");
    gtk_window_set_title(GTK_WINDOW(dialog),"Information");
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

void show_warning_dialog(GtkWidget *widget,gpointer window)
{
    GtkWidget *dialog;
    dialog=gtk_message_dialog_new(window,GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_WARNING,GTK_BUTTONS_OK,"warning infor");
    gtk_window_set_title(GTK_WINDOW(dialog),"Warning");
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

int main(int argc, char **argv)
{
    GtkWidget *window;//主窗口
    GtkWidget *vbox;  //组装盒
    GtkWidget *button;
    
    gtk_init(&argc,&argv);
    
    window=gtk_window_new(GTK_WINDOW_TOPLEVEL);//参数表示创建主窗口
    g_signal_connect_swapped(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),G_OBJECT(window));
    gtk_window_set_title(GTK_WINDOW(window),"Dialog Demo");
    
    vbox=gtk_vbox_new(FALSE,6);
    gtk_container_add(GTK_CONTAINER(window),vbox);
    
    button=gtk_button_new_with_label("Exit");
    gtk_box_pack_start(GTK_BOX(vbox),button,TRUE,TRUE,0);
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(button_event),NULL);
    gtk_widget_show(button);
    
    button=gtk_button_new_with_label("Infor");
    gtk_box_pack_start(GTK_BOX(vbox),button,TRUE,TRUE,0);
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(show_info_dialog),(gpointer)window);
    gtk_widget_show(button);
    
    button=gtk_button_new_with_label("Warning");
    gtk_box_pack_start(GTK_BOX(vbox),button,TRUE,TRUE,0);
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(show_warning_dialog),(gpointer)window);
    gtk_widget_show(button);
    
    gtk_widget_show_all(window);
    
    gtk_main();
    
    return 0;
}

 9.文本选择控件

//创建函数
GtkWidget *gtk_file_selection_new(const gchar *title)

//属性设置
void gtk_file_selection_set_filename(GtkFileSelection *filesel,const gchar *filename);//设置默认路径,filesel文件选择控件标识,filename设置的目录和文件名

gchar* gtk_file_selection_get_filename(gtkfileselection *filesel);//获取所选文件

void gtk_file_selection_show_fileop_buttons(GtkFileSelection *filesel)//显示文件按钮

void gtk_file_selection_set_select_multiple(GtkFileSelection *filesel,gboolean select_multiple)//设置文件多选模式

例子:

View Code
#include <stdio.h>
#include <gtk/gtk.h>

void file_ok_sel(GtkWidget *w,GtkFileSelection *fs)
{
    g_print("%s\n",gtk_file_selection_get_filename(GTK_FILE_SELECTION(fs)));//获取文件路径
}

int main(int argc, char **argv)
{
    GtkWidget *filesel;
    
    gtk_init(&argc,&argv);
    
    filesel=gtk_file_selection_new("File Selection");
    g_signal_connect(G_OBJECT(filesel),"destroy",G_CALLBACK(gtk_main_quit),NULL);
    g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button),"clicked",G_CALLBACK(file_ok_sel),(gpointer)filesel);
    g_signal_connect_swapped(G_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button),"clicked",G_CALLBACK(gtk_widget_destroy),G_OBJECT(filesel));
    
    //gtk_file_selection_set_filename(GTK_FILE_SELECTION(filesel),"img.jpg");
    
    gtk_widget_show_all(filesel);
    
    gtk_main();
    
    return 0;
}

 10.菜单

  GTK中创建菜单需要三个子項,分别为:

  • 菜单項GtkMenuItem:就是用户具体要单击的对象
  • 菜单Menu:菜单項的容器,用于将创建好的菜单項添加到菜单中
  • 菜单栏GtkMenuBar:菜单的容器,用于组织各个菜单
//创建菜单
GtkWIdget* gtk_menu_new(void)

//创建菜单项
GtkWidget* gtk_menu_item_new(void)
GtkWidget* gtk_menu_item_new_with_label(const gchar *label)
GtkWidget* gtk_menu_item_new_with_mnemonic(const gchar *label)

//创建菜单栏
GtkWidget* gtk_menu_new(void)

  下面介绍如何将上面的菜单栏,菜单和菜单項组合成一个菜单控件。

  要将菜单項放到一个菜单中,首先需要创建一个菜单的根菜单項,即根条目,在菜单被下拉时才显示所有的菜单項。其他菜单項则组织为根菜单項的子菜单。

  可以使用追加或者插入等函数将菜单放到一个菜单中,具体函数宏定义如下:

//将菜单項追加到菜单項列表的最后位置
#define gtk_menu_append(menu,child) gtk_menu_shell_append((GtkMenuShell *)(menu),(child))
//将菜单項添加到菜单項列表的开始位置
#define gtk_menu_prepend(menu,child) gtk_menu_shell_prepend((GtkMenuShell *)(menu),(child))
//将菜单項添加到菜单項列表的指定位置
#define gtk_menu_insert(menu,child,pos) gtk_menu_shell_insert((GtkMenuShell *)(menu),(child),(pos))


//将根菜单項与子菜单項进行关联
void gtk_menu_item_set_submenu(GtkMenuItem *menu_item,GtkWidget* submenu)


//将菜单項加到菜单栏
#define gtk_menu_bar_append(menu,child) gtk_menu_shell_append((GtkMenuShell *)(menu),(child))
#define gtk_menu_bar_prepend(menu,child) gtk_menu_shell_prepend((GtkMenuShell *)(menu),(child))
#define gtk_menu_bar_insert(menu,child,pos) gtk_menu_shell_insert((GtkMenuShell *)(menu),(child),(pos))

总结主要步骤:

  1. 用gtk_menu_new()函数创建一个新的菜单
  2. 创建菜单上需要的菜单项,将菜单項添加到菜单上
  3. 用gtk_menu_item_new()函数创建一个根菜单項
  4. 用gtk_menu_item_set_submenu()函数将菜单绑定到根菜单項
  5. 用gtk_menu_bar_new()函数创建一个新的菜单栏
  6. 用gtk_menu_bar_append()函数将根菜单項放到菜单栏上

 例子:

View Code
//MENU_demo.c
#include <stdio.h>
#include <gtk/gtk.h>

int main(int argc, char **argv)
{
    GtkWidget *window;
    GtkWidget *h_box;
    GtkWidget *menu_item,*root_menuitem;
    GtkWidget *menu;
    GtkWidget *menu_bar;
    
    gtk_init(&argc,&argv);
    
    window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_usize(window,400,200);
    g_signal_connect(GTK_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);
    
    h_box=gtk_vbox_new(FALSE,5);
    gtk_container_add(GTK_CONTAINER(window),h_box);
    
    menu_bar=gtk_menu_bar_new();
    gtk_box_pack_start(GTK_BOX(h_box),menu_bar,FALSE,TRUE,0);
    
    
    menu=gtk_menu_new();
    
    menu_item=gtk_menu_item_new_with_label("打开");
    gtk_menu_append(GTK_MENU(menu),menu_item);
    menu_item=gtk_menu_item_new_with_label("保存");
    gtk_menu_append(GTK_MENU(menu),menu_item);
    menu_item=gtk_menu_item_new_with_label("退出");
    gtk_menu_append(GTK_MENU(menu),menu_item);
    g_signal_connect(GTK_OBJECT(menu_item),"active",G_CALLBACK(gtk_main_quit),NULL);
    
    root_menuitem=gtk_menu_item_new_with_label("文件");
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(root_menuitem),menu);
    gtk_menu_bar_append(GTK_MENU_BAR(menu_bar),root_menuitem);
    root_menuitem=gtk_menu_item_new_with_label("编辑");
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(root_menuitem),menu);
    gtk_menu_bar_append(GTK_MENU_BAR(menu_bar),root_menuitem);
    root_menuitem=gtk_menu_item_new_with_label("帮助");
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(root_menuitem),menu);
    gtk_menu_bar_append(GTK_MENU_BAR(menu_bar),root_menuitem);    
    
    gtk_widget_show_all(window);
    
    gtk_main();
    
    return 0;
}

11.容器与布局

组装盒:

//创建组装盒
GtkWIght* gtk_hbox_new(gboolean homogeneous, gint spacing)//横向
GtkWIght* gtk_vbox_new(gboolean homogeneous, gint spacing)//纵向

//加入控件
void gtk_box_pack_start(GtkBox *box,GtkWidget *child,gboolean expand,gboolean fill,guint padding);//放在顶部或左边
void gtk_box_pack_end(GtkBox *box,GtkWidget *child,gboolean expand,gboolean fill,guint padding);//放在下部或右边
//box为组装盒,expend为可填充控件,fill是否充分利用空间,padding控件周围保留的间距

//除此之外,还有按钮盒(GtkButtonBox)

表格:

//创建表格
GtkWidget* gtk_table_new(guint rows, guint columns, gboolean homogeneous)

//添加控件表格的参数
void gtk_table_attach(GtkTable *table, GtkWidget *child, guint left_attach,guint right_attach,guint top_attach,guint botton_attach,GtkAttahOptions xoptions,GtkAttachOptions yoptions,guint xpadding,guint ypadding);
void gtk_table_attach_default(GtkTable *table, GtkWidget *child, guint left_attach,guint right_attach,guint top_attach,guint botton_attach);
//table表名,child控件名

//属性设置
void gtk_table_set_row_spacing(...)
guint gtk_table_get_row_spacing(...)

void gtk_table_set_col_spacing(...)
guint gtk_table_get_col_spacing(...)

guint gtk_table_get_default_row_spacing(...)
guint gtk_table_get_default_col_spacing(...)

void gtk_table_set_homogeneous(...)
gboolean gtk_table_get_homogeneous(...)

此外,还有固定容器(GtkFixed),布局容器(GtkLayout),分割面板容器(GtkPaned),分页容器(GtkNotebook),这四种容器可以参考这里

知识共享许可协议
linux GTK教程(消息机制/标签/按钮/图像/文本/对话框/菜单/容器)cococo点点 创作,采用 知识共享 署名-非商业性使用-相同方式共享 3.0 中国大陆 许可协议进行许可。欢迎转载,请注明出处:
转载自:cococo点点 http://www.cnblogs.com/coder2012
posted @ 2013-04-05 19:36  cococo点点  阅读(10378)  评论(1编辑  收藏  举报