| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5498 人关注过本帖, 2 人收藏
标题:GTK+实例学习(笔记)
只看楼主 加入收藏
guixiaolan
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:356
专家分:0
注 册:2006-4-20
收藏
得分:0 
触发按钮
程序代码:
#include <gtk/gtk.h>

int main(int argc,char *argv[])
{

 GtkWidget *window;

 GtkWidget *button;

 gtk_init(&argc,&argv);

 window=gtk_window_new(GTK_WINDOW_TOPLEVEL);

 gtk_signal_connect(GTK_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);


 button=gtk_toggle_button_new_with_label("Button");//触发按钮

 gtk_container_add(GTK_CONTAINER(window),button);

 gtk_widget_show(button);

 gtk_widget_show(window);

 gtk_main();

 return 0;
}

[url=http://www./]/sign/free.gif" target="_blank">http://ip./sign/free.gif[/url]
2008-09-20 21:36
guixiaolan
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:356
专家分:0
注 册:2006-4-20
收藏
得分:0 
触发按钮,带复选框
程序代码:
#include <gtk/gtk.h>

int main(int argc,char *argv[])
{

 GtkWidget *window;

 GtkWidget *button;

 gtk_init(&argc,&argv);

 window=gtk_window_new(GTK_WINDOW_TOPLEVEL);

 gtk_signal_connect(GTK_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);


 button=gtk_check_button_new_with_label("Button");//触发按钮,带复选框

 gtk_container_add(GTK_CONTAINER(window),button);

 gtk_widget_show(button);

 gtk_widget_show(window);

 gtk_main();

 return 0;
}


Screenshot-011.png (2.21 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册

[url=http://www./]/sign/free.gif" target="_blank">http://ip./sign/free.gif[/url]
2008-09-20 21:37
guixiaolan
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:356
专家分:0
注 册:2006-4-20
收藏
得分:0 
带标签的按钮
程序代码:
#include <gtk/gtk.h>

int main(int argc,char *argv[])
{

 GtkWidget *window;

 GtkWidget *button;

 GtkWidget *box;

 GSList *group=NULL;//组,初始设为NULL


 gtk_init(&argc,&argv);

 window=gtk_window_new(GTK_WINDOW_TOPLEVEL);

 gtk_signal_connect(GTK_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);

 box=gtk_vbox_new(FALSE,0);


 button=gtk_radio_button_new(group);//创建一个不带标签的选择按钮

 group=gtk_radio_button_group(GTK_RADIO_BUTTON(button));//将按钮放入到组中

 gtk_box_pack_start(GTK_BOX(box),button,FALSE,FALSE,0);//将按钮加入到组合框中

 gtk_widget_show(button);


 button=gtk_radio_button_new_with_label(group,"Radio Button2");//创建一个带标签的按钮

 group=gtk_radio_button_group(GTK_RADIO_BUTTON(button));

 gtk_box_pack_start(GTK_BOX(box),button,FALSE,FALSE,0);

 gtk_widget_show(button);


 gtk_container_add(GTK_CONTAINER(window),box);

 gtk_widget_show(box);

 gtk_widget_show(window);

 gtk_main();

 return 0;
}


Screenshot-012.png (3.86 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册

[url=http://www./]/sign/free.gif" target="_blank">http://ip./sign/free.gif[/url]
2008-09-20 21:38
guixiaolan
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:356
专家分:0
注 册:2006-4-20
收藏
得分:0 
标签构件
程序代码:
#include <gtk/gtk.h>

int main(int argc,char *argv[])
{

 GtkWidget *window;

 GtkWidget *label;

 gtk_init(&argc,&argv);

 window=gtk_window_new(GTK_WINDOW_TOPLEVEL);

 gtk_signal_connect(GTK_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);

 label=gtk_label_new("This is a label");//创建标签构件

 gtk_widget_show(label);


 gtk_container_add(GTK_CONTAINER(window),label);

 gtk_widget_show(window);

 gtk_main();

 return 0;
}


Screenshot-013.png (2.6 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册

[url=http://www./]/sign/free.gif" target="_blank">http://ip./sign/free.gif[/url]
2008-09-20 21:38
guixiaolan
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:356
专家分:0
注 册:2006-4-20
收藏
得分:0 
程序代码:
#include <gtk/gtk.h>

void button_event(GtkWidget *,gpointer *);
GtkWidget *label;

int main(int argc,char *argv[])
{

 GtkWidget *window;

 GtkWidget *button;

 GtkWidget *box;

 GSList *group=NULL;


 gtk_init(&argc,&argv);

 window=gtk_window_new(GTK_WINDOW_TOPLEVEL);

 gtk_signal_connect(GTK_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);

 box=gtk_vbox_new(FALSE,0);

 label=gtk_label_new("Please check!");

 gtk_box_pack_start(GTK_BOX(box),label,FALSE,FALSE,30);


 button=gtk_radio_button_new_with_label(group,"Radio Button1");

 group=gtk_radio_button_group(GTK_RADIO_BUTTON(button));

 gtk_box_pack_start(GTK_BOX(box),button,FALSE,FALSE,0);

 gtk_signal_connect(GTK_OBJECT(button),"pressed",GTK_SIGNAL_FUNC(button_event),"You check button1");

 //单击按钮时标签标号发生变化

 gtk_widget_show(button);


 button=gtk_radio_button_new_with_label(group,"Radio Button2");

 group=gtk_radio_button_group(GTK_RADIO_BUTTON(button));

 gtk_box_pack_start(GTK_BOX(box),button,FALSE,FALSE,0);

 gtk_signal_connect(GTK_OBJECT(button),"pressed",GTK_SIGNAL_FUNC(button_event),"You check button2");

 gtk_widget_show(button);


 gtk_container_add(GTK_CONTAINER(window),box);

 gtk_widget_show(label);

 gtk_widget_show(box);

 gtk_widget_show(window);

 gtk_main();

 return 0;
}

void button_event(GtkWidget *widget,gpointer *data)
{

 gtk_label_set(GTK_LABEL(label),(char *)data);
}


Screenshot-014.png (5.23 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册

[url=http://www./]/sign/free.gif" target="_blank">http://ip./sign/free.gif[/url]
2008-09-20 21:38
guixiaolan
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:356
专家分:0
注 册:2006-4-20
收藏
得分:0 
程序代码:
#include <gtk/gtk.h>

int main(int argc,char *argv[])
{

 GtkWidget *window;

 GtkWidget *label;


 gtk_init(&argc,&argv);

 window=gtk_window_new(GTK_WINDOW_TOPLEVEL);

 gtk_widget_set_usize(window,200,200);

 gtk_signal_connect(GTK_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);


 label=gtk_label_new("This is a label");

 gtk_widget_set_usize(label,100,10);//标签大小


 gtk_label_set_justify(GTK_LABEL(label),GTK_JUSTIFY_CENTER);//设置文本对齐方式

 gtk_label_set_line_wrap(GTK_LABEL(label),TRUE);//文本可自动换行

 gtk_widget_show(label);


 gtk_container_add(GTK_CONTAINER(window),label);

 gtk_widget_show(window);

 gtk_main();

 return 0;
}


Screenshot-015.png (3.87 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册

[url=http://www./]/sign/free.gif" target="_blank">http://ip./sign/free.gif[/url]
2008-09-20 21:39
guixiaolan
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:356
专家分:0
注 册:2006-4-20
收藏
得分:0 
程序代码:
#include <gtk/gtk.h>

int main(int argc,char *argv[])
{

 GtkWidget *window;

 GtkWidget *event_box;

 GtkWidget *label;


 gtk_init(&argc,&argv);

 window=gtk_window_new(GTK_WINDOW_TOPLEVEL);

 gtk_signal_connect(GTK_OBJECT(window),"destroy",GTK_SIGNAL_FUNC(gtk_main_quit),NULL);

 gtk_container_set_border_width(GTK_CONTAINER(window),10);


 event_box=gtk_event_box_new();//事件盒构件

 gtk_container_add(GTK_CONTAINER(window),event_box);

 gtk_widget_show(event_box);


 label=gtk_label_new("Click here to quit");

 gtk_container_add(GTK_CONTAINER(event_box),label);

 gtk_widget_show(label);

 gtk_signal_connect(GTK_OBJECT(event_box),"button_press_event",GTK_SIGNAL_FUNC(gtk_main_quit),NULL);

 //单击标签时发生事件

 gtk_widget_show(window);

 gtk_main();

 return 0;
}


Screenshot-016.png (3.62 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册

[url=http://www./]/sign/free.gif" target="_blank">http://ip./sign/free.gif[/url]
2008-09-20 21:39
guixiaolan
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:356
专家分:0
注 册:2006-4-20
收藏
得分:0 
程序代码:
#include <gtk/gtk.h>

void button_event(GtkWidget *,gpointer *);
GtkWidget *name;

int main(int argc,char *argv[])
{

 GtkWidget *window;

 GtkWidget *button;

 GtkWidget *box;

 GtkWidget *label;


 gtk_init(&argc,&argv);

 window=gtk_window_new(GTK_WINDOW_TOPLEVEL);

 gtk_signal_connect(GTK_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);


 box=gtk_vbox_new(FALSE,0);

 label=gtk_label_new("Name");

 gtk_box_pack_start(GTK_BOX(box),label,FALSE,FALSE,5);

 gtk_widget_show(label);


 name=gtk_entry_new();//录入构件

 gtk_box_pack_start(GTK_BOX(box),name,FALSE,FALSE,5);

 gtk_entry_prepend_text(GTK_ENTRY(name),"Please input your name!");//初始化录入构件框

 gtk_widget_show(name);

 

 button=gtk_button_new_with_label("enter");

 gtk_box_pack_start(GTK_BOX(box),button,FALSE,FALSE,5);

 gtk_signal_connect(GTK_OBJECT(button),"pressed",GTK_SIGNAL_FUNC(button_event),"enter");

 gtk_widget_show(button);


 gtk_container_add(GTK_CONTAINER(window),box);

 gtk_widget_show(box);

 gtk_widget_show(window);

 gtk_main();

 return 0;
}

void button_event(GtkWidget *widget,gpointer *data)
{

 const gchar *uname;

 uname=gtk_entry_get_text(GTK_ENTRY(name));//取得录入的内容并打印

 g_print("Name:%s\n",uname);
}


Screenshot-017.png (6.11 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册

[url=http://www./]/sign/free.gif" target="_blank">http://ip./sign/free.gif[/url]
2008-09-20 21:40
guixiaolan
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:356
专家分:0
注 册:2006-4-20
收藏
得分:0 
程序代码:
#include <gtk/gtk.h>

void button_event(GtkWidget *,gpointer *);
GtkWidget *name;
GtkWidget *passwd;

int main(int argc,char *argv[])
{

 GtkWidget *window;

 GtkWidget *button;

 GtkWidget *box;

 GtkWidget *label;


 gtk_init(&argc,&argv);

 window=gtk_window_new(GTK_WINDOW_TOPLEVEL);

 gtk_signal_connect(GTK_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);


 box=gtk_vbox_new(FALSE,0);

 label=gtk_label_new("name");

 gtk_box_pack_start(GTK_BOX(box),label,FALSE,FALSE,5);

 gtk_widget_show(label);

 

 name=gtk_entry_new();

 gtk_box_pack_start(GTK_BOX(box),name,FALSE,FALSE,5);

 gtk_widget_show(name);


 label=gtk_label_new("passwd");

 gtk_box_pack_start(GTK_BOX(box),label,FALSE,FALSE,5);

 gtk_widget_show(label);


 passwd=gtk_entry_new();

 gtk_entry_set_visibility(GTK_ENTRY(passwd),FALSE);//录入内容不可见,如密码等

 gtk_box_pack_start(GTK_BOX(box),passwd,FALSE,FALSE,5);

 gtk_widget_show(passwd);


 button=gtk_button_new_with_label("enter");

 gtk_box_pack_start(GTK_BOX(box),button,FALSE,FALSE,5);

 gtk_signal_connect(GTK_OBJECT(button),"pressed",GTK_SIGNAL_FUNC(button_event),"enter");

 gtk_widget_show(button);


 gtk_container_add(GTK_CONTAINER(window),box);

 gtk_widget_show(box);

 gtk_widget_show(window);

 gtk_main();

 return 0;
}

void button_event(GtkWidget *widget,gpointer *data)
{

 const gchar *uname;

 const gchar *upasswd;

 uname=gtk_entry_get_text(GTK_ENTRY(name));

 upasswd=gtk_entry_get_text(GTK_ENTRY(passwd));

 g_print("Name:%s\n",uname);

 g_print("Passwd:%s\n",upasswd);
}


Screenshot-018.png (6.88 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册

[url=http://www./]/sign/free.gif" target="_blank">http://ip./sign/free.gif[/url]
2008-09-20 21:40
guixiaolan
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:356
专家分:0
注 册:2006-4-20
收藏
得分:0 
程序代码:
#include <gtk/gtk.h>

GtkWidget *CreateMenuItem(GtkWidget *MenuBar,char *test);
GtkWidget *CreateMenu(GtkWidget *MenuItem);

int main(int argc,char *argv[])
{

 GtkWidget *window;

 GtkWidget *MenuBar;

 GtkWidget *box;

 GtkWidget *MenuItemFile;


 gtk_init(&argc,&argv);

 window=gtk_window_new(GTK_WINDOW_TOPLEVEL);

 gtk_widget_set_usize(window,400,200);

 gtk_signal_connect(GTK_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);


 box=gtk_vbox_new(FALSE,0);

 gtk_container_add(GTK_CONTAINER(window),box);

 MenuBar=gtk_menu_bar_new();//创建菜单条

 gtk_box_pack_start(GTK_BOX(box),MenuBar,FALSE,TRUE,0);

 


 MenuItemFile=CreateMenuItem(MenuBar,"File");//创建菜单项

 CreateMenu(MenuItemFile);//创建子菜单

 CreateMenuItem(MenuBar,"Edit");

 CreateMenuItem(MenuBar,"View");

 CreateMenuItem(MenuBar,"Insert");

 CreateMenuItem(MenuBar,"Tool");


 gtk_widget_show(box);

 gtk_widget_show(MenuBar);

 gtk_widget_show(window);

 gtk_main();

 return 0;
}

GtkWidget *CreateMenuItem(GtkWidget *MenuBar,char *test)
{

 GtkWidget *MenuItem;

 MenuItem=gtk_menu_item_new_with_label(test);//创建菜单项

 gtk_menu_shell_append(GTK_MENU_SHELL(MenuBar),MenuItem);//将菜单项放入到菜单条中

 gtk_widget_show(MenuItem);

 return MenuItem;
}

GtkWidget *CreateMenu(GtkWidget *MenuItem)
{

 GtkWidget *Menu;

 GtkWidget *Exit;

 Menu=gtk_menu_new();//创建子菜单

 CreateMenuItem(Menu,"New");//创建子菜单项

 CreateMenuItem(Menu,"Open");

 CreateMenuItem(Menu,"Save");

 Exit=CreateMenuItem(Menu,"Exit");//退出

 g_signal_connect(GTK_OBJECT(Exit),"activate",G_CALLBACK(gtk_main_quit),NULL);

 gtk_menu_item_set_submenu(GTK_MENU_ITEM(MenuItem),Menu);//将子菜单放入到父菜单中

 gtk_widget_show(Menu);

 return Menu;
}


Screenshot-019.png (5.33 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册

[url=http://www./]/sign/free.gif" target="_blank">http://ip./sign/free.gif[/url]
2008-09-20 21:41
快速回复:GTK+实例学习(笔记)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034272 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved