linuxGTK教程(消息机制标签按钮图像⽂本对话框菜单容器)
  GTK+(GIMP Toolkit)是⼀套源码以LGPL许可协议分发、跨平台的图形⼯具包。最初是为GIMP写的,已成为⼀个功能强⼤、设计灵活的⼀个通⽤图形库,是GNU/Linux下开发图形界⾯的应⽤程序的主流开发⼯具之⼀。
1.
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();
return0;
}
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();
return0;
}
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();
return0;
}
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();
return0;
}
//注意:编译时候,要添加-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)linux换行按哪个键
//添加按钮
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();
return0;
}
9.⽂本选择控件
//创建函数
GtkWidget *gtk_file_selection_new(const gchar *title)
//属性设置
void gtk_file_selection_set_filename(GtkFileSelection *filesel,const gchar *filename);//设置默认路径,filesel⽂件选择控件标识,filename设置的⽬录和⽂件名

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。