[Gtk programming]Teach you to create check menu item, radio menu item

发布于 2022-07-30 03:42:44 字数 865 浏览 11 评论 5

This article will teach you how to create pop-up menu, sub menu, check menu item, radio menu item, separator menu item, and make a menu item grayed by GTK. the reason for I write this article is I did not found any document about pop-up menu, check menu item, radio menu item in GTK 2.0 reference manual. I hope the article will be useful for you.Thank you for viewing.I will be so sorry if my bad English writting confused you.

In order to create a menu, please call the gtk_menu_new() function;this function returns a pointer to a menu widget.

GtkWidget *gtk_menu_new( void );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

离去的眼神 2022-08-12 04:47:48

这篇文章将教你如何用GTK创建弹出菜单,子菜单,复选菜单项,单选菜单项,分隔菜单项,以及如何让一个菜单项变灰。我在GTK 2.0参考手册上没有找到关于弹出菜单项,复选菜单项,单选菜单项的文档,所以就写了这篇文章。希望对大家有用,谢谢看贴。我的英文写作能力很差,希望不要把你们搞晕了。

    要创建一个菜单,请调用gtk_menu_new() 函数,这个函数返回一个指向菜单构件的指针。

GtkWidget *gtk_menu_new( void );

遮了一弯 2022-08-12 04:45:48

编译用

gcc RadioMenuItem.c GtkMenu.c `pkg-config --cflags --libs gtk+-2.0`

允世 2022-08-12 02:19:35
#include "GtkMenu.h"

/*
* Function:on_button_press_event
* Description: call back funtion of the button press event
* Returns: TRUE if the mouse right button be clicked, otherwise, retuns FALSE
*/

gboolean on_button_press_event(GtkWidget *widget, GdkEventButton *event)
{
    if (event-> button ==3)
    {
        gtk_menu_popup (GTK_MENU(CreatePopupMenu()), NULL, NULL, NULL, NULL, event->button, event->time);
        return TRUE;
    }
    return FALSE;
}

int main( int   argc, char *argv[] )
{
    GtkWidget *window;
    GtkWidget *button;
    GtkWidget *menu_items;

    gtk_init (&argc, &argv);

    /* create a new window */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "GTK Radio Menu Item Test");
    gtk_widget_set_usize(GTK_WIDGET(window), 300, 300);
    g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL);
   
    /* create a button*/
    button = gtk_button_new_with_label( "right click here to display the pop-up menu" );
    g_signal_connect (G_OBJECT (button), "button_press_event",G_CALLBACK (on_button_press_event), NULL);

        gtk_container_add(GTK_CONTAINER(window), button);   
        
    /* display the window */
    gtk_widget_show_all (window);
    gtk_main ();
    return 0;
}

七颜 2022-08-12 00:56:37
#include "GtkMenu.h"
#include <stdio.h>

void on_left_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
    printf("Leftn");
}

void on_top_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
    printf("Topn");
}

void on_right_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
    printf("Rightn");
}

void on_bottom_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
    printf("Bottomn");
}

void on_toolbar_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
    printf("Toolbarn");
}

void on_statusbar_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
    printf("Statusbarn");
}

void on_normal_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
    printf("Normal menuitemn");
}

void on_normal_menuitem2_active(GtkMenuItem *menuitem, gpointer data)
{
    printf("Normal menuitem2n");
}

void on_high_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
    printf("Highn");
}

void on_middle_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
    printf("middlen");
}

void on_low_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
    printf("Lown");
}

void on_popup_menuitem_active(GtkMenuItem *menuitem, gpointer data)
{
}

/*
* Function: CreateMenuItem
* Description: the function create a normal menu item
* Parameters:menuitemtext[in] menu text to display
                             func [in] call back function of the menu item
* Returns: a pointer to a menu item widget
*/
        
GtkWidget* CreateMenuItem(gchar* menuitemtext, GCallback func)
{
    GtkWidget *menuitem = gtk_menu_item_new_with_label (menuitemtext);
    g_signal_connect( menuitem, "activate", G_CALLBACK(func), NULL);
    return menuitem;
}

/*
* Function: CreateRadioMenuItem
* Description: the function create a radio menu item
* Parameters:group[in] the radio menu item will be created will append to this group
                             menuitemtext[in] menu text to display
                             func [in] call back function of the menu item
* Returns: a pointer to a radio menu item widget
*/
        
GtkWidget* CreateRadioMenuItem(GSList* group, gchar* menuitemtext, GCallback func)
{
    GtkWidget *menuitem = gtk_radio_menu_item_new_with_label (group, menuitemtext);
    g_signal_connect( menuitem, "activate", G_CALLBACK(func), NULL);
    return menuitem;
}

/*
* Function: CreateSeparatorMenuItem
* Description: the function create a separator menu item
* Parameters:none
* Returns: a pointer to a separator menu item widget
*/
        
GtkWidget* CreateSeparatorMenuItem()
{
    return gtk_separator_menu_item_new();
}

/*
* Function: CreateCheckMenuItem
* Description: the function create a check menu item
* Parameters:menuitemtext[in] menu text to display; func [in] call back function of the menu item;  checked[in] which takes a TRUE or FALSE value and decide to check or uncheck the menu item
* Returns: a pointer to a check menu item widget
*/
                    
GtkWidget* CreateCheckMenuItem(gchar* menuitemtext, GCallback func, gboolean checked)
{
    GtkWidget* menuitem = gtk_check_menu_item_new_with_label (menuitemtext);
    g_signal_connect( menuitem, "activate", G_CALLBACK(func), NULL);
    gtk_check_menu_item_set_active((GtkCheckMenuItem *)menuitem, checked);
    return menuitem;
}

/*
* Function: CreateSubMenu
* Description: the function create a submenu
* Parameter:none
* Returns: a pointer to a menu widget
*/

GtkWidget* CreateSubMenu()
{
  GtkWidget* menu = gtk_menu_new ();
  GtkWidget* menu_item = CreateRadioMenuItem(NULL, "High", GTK_SIGNAL_FUNC(on_high_menuitem_active));
  GtkWidget* normal_menu_item;
  GSList* group = gtk_radio_menu_item_get_group((GtkRadioMenuItem *)menu_item);
  gtk_menu_shell_append (GTK_MENU_SHELL(menu), menu_item);
  gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateRadioMenuItem(group, "Middle", GTK_SIGNAL_FUNC(on_middle_menuitem_active)));
  gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateRadioMenuItem(group, "Low", GTK_SIGNAL_FUNC(on_low_menuitem_active)));
  gtk_widget_show_all(menu);
  return menu;
}

/*
* Function: CreatePopupMenu
* Description: the function create a pop-up menu
* Parameter:none
* Returns: a pointer to a menu widget
*/

GtkWidget* CreatePopupMenu()
{
    /* create a menu */
    GtkWidget* menu = gtk_menu_new ();
  GtkWidget* menu_item = CreateRadioMenuItem(NULL, "Left", GTK_SIGNAL_FUNC(on_left_menuitem_active));
  GtkWidget* normal_menu_item;
  GtkWidget* popmenuitem;
  /* append radio menu items to the menu */
  GSList* group = gtk_radio_menu_item_get_group((GtkRadioMenuItem *)menu_item);
  gtk_menu_shell_append (GTK_MENU_SHELL(menu), menu_item);
  gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateRadioMenuItem(group, "Top", GTK_SIGNAL_FUNC(on_top_menuitem_active)));
  gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateRadioMenuItem(group, "Right", GTK_SIGNAL_FUNC(on_right_menuitem_active)));
  gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateRadioMenuItem(group, "Bottom", GTK_SIGNAL_FUNC(on_bottom_menuitem_active)));
  
  /* append separator menu item to the menu */
  gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateSeparatorMenuItem());
  
  /* append check menu items to the menu */
    gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateCheckMenuItem("Toolbar", GTK_SIGNAL_FUNC(on_toolbar_menuitem_active), TRUE));
    gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateCheckMenuItem("Statusbar", GTK_SIGNAL_FUNC(on_statusbar_menuitem_active), FALSE));
  gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateSeparatorMenuItem());
  
  /* append normal menu items to the menu */
  normal_menu_item = CreateMenuItem("Normal menuitem", GTK_SIGNAL_FUNC(on_normal_menuitem_active));
  gtk_menu_shell_append (GTK_MENU_SHELL(menu), normal_menu_item);
  
  /* disable a normal menu item */
  gtk_widget_set_sensitive(GTK_WIDGET(normal_menu_item), FALSE);
  
  gtk_menu_shell_append (GTK_MENU_SHELL(menu), CreateMenuItem("Normal menuitem2", GTK_SIGNAL_FUNC(on_normal_menuitem2_active)));
  
  /* create a submenu and append to a menu item */
  popmenuitem = CreateMenuItem("Popup", GTK_SIGNAL_FUNC(on_popup_menuitem_active));
  gtk_menu_shell_append (GTK_MENU_SHELL(menu), popmenuitem);
  gtk_menu_item_set_submenu((GtkMenuItem *)popmenuitem, CreateSubMenu());
  
  gtk_widget_show_all(menu);
  return menu;
}

窝囊感情。 2022-08-07 04:06:23
#ifndef __GTKMENU_H__
#define __GTKMENU_H__

#include <gtk/gtk.h>

void on_left_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_top_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_right_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_bottom_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_toolbar_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_statusbar_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_normal_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_normal_menuitem2_active(GtkMenuItem *menuitem, gpointer data);
void on_high_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_middle_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_low_menuitem_active(GtkMenuItem *menuitem, gpointer data);
void on_popup_menuitem_active(GtkMenuItem *menuitem, gpointer data);   
GtkWidget* CreateMenuItem(gchar* menuitemtext, GCallback func);
GtkWidget* CreateRadioMenuItem(GSList* group, gchar* menuitemtext, GCallback func);
GtkWidget* CreateSeparatorMenuItem();   
GtkWidget* CreateCheckMenuItem(gchar* menuitemtext, GCallback func, gboolean checked);
GtkWidget* CreateSubMenu();
GtkWidget* CreatePopupMenu();

#endif

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文