第一个GTK程序例子:
#include stdio.h
#include gtk/gtk.h
int main(int argc, char *argv[])
{
gtk_init(NULL,NULL); //初始化GTK
GtkWindow *window =gtk_window_new(GTK_WINDOW_TOPLEVEL); //新建一个window窗口
gtk_window_set_title(window,"窗口标题"); //设置窗口标题
g_signal_connect(window,"destroy",gtk_main_quit,0); //设置窗口的"destroy"信号(事件)的处理函数 gtk_main_quit()
gtk_widget_show(window); //显示窗口
gtk_main(); //程序阻塞
return 0;
}
一、常用方法
//widget操作函数
void gtk_widget_show(widget);
void gtk_widget_hide(widget);
void gtk_widget_set_sensitive(widget,sensitive);
void gtk_widget_set_size_request(widget,width,height);
//window操作函数
gtk_window_set_resizable(window,resizable);
gtk_window_set_position(window,GTK_WIN_POS_CENTER_ALWAYS);
gtk_window_maximize(window);
gtk_container_add(container,widget); //将widget装到container中
gtk_AAA_get_BBB();
gtk_AAA_set_BBB();
//gtk_widget_set_title(widget,"标题");
二、GtkBox(盒子)布局
//创建盒子
GtkBox *box1=gtk_box_new(GTK_ORIENTATION_HORIZONTAL,0);
//GtkBox *box1=gtk_box_new(GTK_ORIENTATION_VERTICAL,0);
gtk_container_add(window,box1);
gtk_widget_show(box1);
//添加按钮1
GtkButton *btn1=gtk_button_new();
gtk_button_set_label(btn1,"按钮1");
gtk_box_pack_start(box1,btn1,FALSE,FALSE,0);
gtk_widget_show(btn1);
//添加按钮2
GtkButton *btn2=gtk_button_new();
gtk_button_set_label(btn2,"按钮2");
gtk_box_pack_start(box1,btn2,FALSE,FALSE,0);
gtk_widget_show(btn2);
//添加文本框1
GtkEntry *entry1=gtk_entry_new();
gtk_box_pack_start(box1,entry1,FALSE,FALSE,0);
gtk_widget_show(entry1);
三、网格布局
/*
GtkGrid *grid1=gtk_grid_new();
gtk_container_add(window,grid1);
gtk_widget_show(grid1);
//gtk_grid_attack(GtkGrid,widget,int left,int top,int width,int height);
//添加按钮1
GtkButton *btn1=gtk_button_new();
gtk_button_set_label(btn1,"按钮1");
gtk_grid_attach(grid1,btn1,0,0,1,1);
gtk_widget_show(btn1);
//添加按钮2
GtkButton *btn2=gtk_button_new();
gtk_button_set_label(btn2,"按钮2");
gtk_grid_attach(grid1,btn2,1,0,1,1);
gtk_widget_show(btn2);
//添加按钮3
GtkButton *btn3=gtk_button_new();
gtk_button_set_label(btn3,"按钮3");
gtk_grid_attach(grid1,btn3,0,1,2,2);
gtk_widget_show(btn3);
*/
附:以上内容为本人学习了杨中科老师的《C语言也能干大事(第三版)》的教学视频之后,对所学的内容进行的总结。