注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

随机过程

http://superware.blog.163.com

 
 
 

日志

 
 

Menu in GTK  

2014-07-14 15:55:50|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

1. 菜单条、菜单、菜单选项的关系

GtkWidget *menu_bar;
GtkWidget *file_menu;
GtkWidget *file_item;

GtkWidget *quit_item;
GtkWidget *exit_item;


menu_bar = gtk_menu_bar_new ();


file_menu = gtk_menu_new ();


file_item = gtk_menu_item_new_with_label ("File");

quit_item = gtk_menu_item_new_with_label ("Quit");
exit_item = gtk_menu_item_new_with_label ("Exit");

// 将 file_item 绑定到 menu_bar

gtk_menu_shell_append (GTK_MENU_SHELL(menu_bar), file_item);
// 将 file_menu 绑定到 file_item
gtk_menu_item_set_submenu (GTK_MENU_ITEM(file_item), file_menu);
// 将 quit_item 绑定到 file_menu
gtk_menu_shell_append (GTK_MENU_SHELL(file_menu), quit_item);

// 将 exit_item 绑定到 file_menu
gtk_menu_shell_append (GTK_MENU_SHELL(file_menu), exit_item);
1、File 菜单条 》 菜单栏
2、File 菜单 》 File 菜单条
3、Quit 菜单条 》 File 菜单
4、Exit 菜单条 》 File 菜单


运行结果:
Menu in GTK - superware - 随机过程

2. 实例

#include <gtk/gtk.h>


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

GtkWidget *window;
GtkWidget *vbox;

GtkWidget *menu_bar;
GtkWidget *file_menu;
GtkWidget *file_item;
GtkWidget *quit_item;

GtkWidget *view_menu;
GtkWidget *view_item;
GtkWidget *zoom_item;

GtkWidget *zoom_menu;
GtkWidget *in_item;
GtkWidget *out_item;

GtkWidget *in_menu;
GtkWidget *in_1_item;
GtkWidget *in_2_item;
GtkWidget *in_3_item;
GtkWidget *in_4_item;
GtkWidget *in_5_item;
GtkWidget *in_6_item;

GtkWidget *out_menu;
GtkWidget *out_1_item;
GtkWidget *out_2_item;
GtkWidget *out_3_item;
GtkWidget *out_4_item;
GtkWidget *out_5_item;
GtkWidget *out_6_item;


gtk_init(&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size (GTK_WINDOW(window), 250, 200);
gtk_window_set_title (GTK_WINDOW(window), "menu");

vbox = gtk_vbox_new (FALSE, 0);
gtk_container_add (GTK_CONTAINER(window), vbox);

menu_bar = gtk_menu_bar_new ();

file_menu = gtk_menu_new ();
file_item = gtk_menu_item_new_with_label ("File");
quit_item = gtk_menu_item_new_with_label ("Quit");

gtk_menu_shell_append (GTK_MENU_SHELL(menu_bar), file_item);
gtk_menu_item_set_submenu (GTK_MENU_ITEM(file_item), file_menu);
gtk_menu_shell_append (GTK_MENU_SHELL(file_menu), quit_item);

view_menu = gtk_menu_new ();
view_item = gtk_menu_item_new_with_label ("View");
zoom_item = gtk_menu_item_new_with_label ("Zoom");

gtk_menu_shell_append (GTK_MENU_SHELL(menu_bar), view_item);
gtk_menu_item_set_submenu (GTK_MENU_ITEM(view_item), view_menu);
gtk_menu_shell_append (GTK_MENU_SHELL(view_menu), zoom_item);

zoom_menu = gtk_menu_new ();
in_item = gtk_menu_item_new_with_label ("In");
out_item = gtk_menu_item_new_with_label ("Out");

gtk_menu_item_set_submenu (GTK_MENU_ITEM(zoom_item), zoom_menu);
gtk_menu_shell_append (GTK_MENU_SHELL(zoom_menu), in_item);
gtk_menu_shell_append (GTK_MENU_SHELL(zoom_menu), out_item);

in_menu = gtk_menu_new ();
in_1_item = gtk_menu_item_new_with_label ("1");
in_2_item = gtk_menu_item_new_with_label ("2");
in_3_item = gtk_menu_item_new_with_label ("3");
in_4_item = gtk_menu_item_new_with_label ("4");
in_5_item = gtk_menu_item_new_with_label ("5");
in_6_item = gtk_menu_item_new_with_label ("6");

gtk_menu_item_set_submenu (GTK_MENU_ITEM(in_item), in_menu);
gtk_menu_shell_append (GTK_MENU_SHELL(in_menu), in_1_item);
gtk_menu_shell_append (GTK_MENU_SHELL(in_menu), in_2_item);
gtk_menu_shell_append (GTK_MENU_SHELL(in_menu), in_3_item);
gtk_menu_shell_append (GTK_MENU_SHELL(in_menu), in_4_item);
gtk_menu_shell_append (GTK_MENU_SHELL(in_menu), in_5_item);
gtk_menu_shell_append (GTK_MENU_SHELL(in_menu), in_6_item);

out_menu = gtk_menu_new ();
out_1_item = gtk_menu_item_new_with_label ("1");
out_2_item = gtk_menu_item_new_with_label ("2");
out_3_item = gtk_menu_item_new_with_label ("3");
out_4_item = gtk_menu_item_new_with_label ("4");
out_5_item = gtk_menu_item_new_with_label ("5");
out_6_item = gtk_menu_item_new_with_label ("6");

gtk_menu_item_set_submenu (GTK_MENU_ITEM(out_item), out_menu);
gtk_menu_shell_append (GTK_MENU_SHELL(out_menu), out_1_item);
gtk_menu_shell_append (GTK_MENU_SHELL(out_menu), out_2_item);
gtk_menu_shell_append (GTK_MENU_SHELL(out_menu), out_3_item);
gtk_menu_shell_append (GTK_MENU_SHELL(out_menu), out_4_item);
gtk_menu_shell_append (GTK_MENU_SHELL(out_menu), out_5_item);
gtk_menu_shell_append (GTK_MENU_SHELL(out_menu), out_6_item);

gtk_box_pack_start (GTK_BOX(vbox), menu_bar, FALSE, FALSE, 3);

g_signal_connect_swapped (G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);

g_signal_connect (G_OBJECT(quit_item), "activate", G_CALLBACK(gtk_main_quit), NULL);

gtk_widget_show_all (window);

gtk_main ();

return 0;
}

 结果:
  评论这张
 
阅读(48)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017