ncurses 与 menu

ncurses 与 menu

一下位ncurses和菜单库menu的demo程序

#include <menu.h>
#include <ncurses.h>
#include <stdlib.h>
#include <string.h>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4

char *choices[] = {
        "Choice 1",
        "Choice 2",
        "Choice 3",
        "Choice 4",
        "Choice 5",
        "Choice 6",
        "Choice 7",
        "Choice 8",
        "Choice 9",
        "Choice 10",
        "Choice 11",
        "Choice 12",
        "Choice 13",
        "Choice 14",
        "Choice 15",
        "Choice 16",
        "Choice 17",
        "Choice 18",
        "Choice 19",
        "Choice 20",
        "Exit",
        NULL,
};
void print_in_middle(WINDOW *win, int y, int startx, int width, char *string, chtype color);

int main(){
    ITEM **my_items;
    int c;
    MENU *my_menu;
    WINDOW *my_menu_win;
    int n_choices;
    initscr();
    start_color();
    cbreak();
    noecho();
    keypad(stdscr, TRUE);
    curs_set(0);
    init_pair(1, COLOR_RED, COLOR_BLACK);

    n_choices = ARRAY_SIZE(choices);
    my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
    for(int i = 0; i < n_choices; i++)
        my_items[i] = new_item(choices[i], choices[i]);
    item_opts_off(my_items[3], O_SELECTABLE);
    my_menu = new_menu((ITEM **)my_items);

    my_menu_win = newwin(10, 50, 4, 4);
    keypad(my_menu_win, TRUE);

    set_menu_win(my_menu, my_menu_win);
    set_menu_sub(my_menu, derwin(my_menu_win, 6, 48, 3, 1));
    set_menu_mark(my_menu, " * ");
    menu_opts_off(my_menu, O_SHOWDESC);
    menu_opts_off(my_menu, O_ONEVALUE);
    set_menu_format(my_menu, 6, 2);

    box(my_menu_win, 0, 0);
    print_in_middle(my_menu_win, 1, 0, 50, "My Menu", COLOR_PAIR(1));
    mvwhline(my_menu_win, 2, 1, ACS_HLINE, 48);
    mvprintw(LINES - 2, 0, "F1 to exit");
    refresh();

    post_menu(my_menu);
    wrefresh(my_menu_win);
    while((c = wgetch(my_menu_win)) != KEY_F(1))
    { switch(c)
        {
            case KEY_DOWN:
                menu_driver(my_menu, REQ_DOWN_ITEM);
                break;
            case KEY_UP:
                menu_driver(my_menu, REQ_UP_ITEM);
                break;
            case KEY_LEFT:
                menu_driver(my_menu, REQ_LEFT_ITEM);
                break;
            case KEY_RIGHT:
                menu_driver(my_menu, REQ_RIGHT_ITEM);
                break;
            case 'm':
                menu_driver(my_menu, REQ_SCR_DPAGE);
                break;
            case 'n':
                menu_driver(my_menu, REQ_SCR_UPAGE);
                break;
            case ' ':
                menu_driver(my_menu, REQ_TOGGLE_ITEM);
                break;
            case '\n':{
                int count = item_count(my_menu);
                char tmp[200]= {};
                ITEM **item = menu_items(my_menu);
                for (int i=0; i < count; i++) {
                    if (item_value(item[i]) == TRUE) {
                        strcat(tmp, item_name(item[i]));
                        strcat(tmp, " ");
                    }
                }
                move(LINES - 3, 0);
                clrtoeol();
                mvprintw(LINES - 3, 0, "%s", tmp);
                refresh();
                break;
            }
            case 'u':{
                ITEM *item = current_item(my_menu);
                move(LINES - 3, 0);
                clrtoeol();
                mvprintw(LINES - 3, 0, "%s", item_name(item));
                refresh();
                break;
            }
            case 'r':{
                set_current_item(my_menu, my_items[4]);
                break;
            }
        }
        wrefresh(my_menu_win);
    }
    unpost_menu(my_menu);
    free_menu(my_menu);
    for(int i = 0; i < n_choices; ++i)
        free_item(my_items[i]);
    free(my_items);
    endwin();
}

void print_in_middle(WINDOW *win, int y, int startx, int width, char *string, chtype color) {
    int x;

    if(win == NULL)
        win = stdscr;
    if(y == 0)
        getyx(win, y, x);
    x = startx + (width - strlen(string)) / 2;

    wattron(win, color);
    mvwprintw(win, y, x, "%s", string);
    wattroff(win, color);
    refresh();
}

使用方向键可选择选中项,按空格勾选,按回车确定
r显示当前选中项,n将选中Choice 5

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桓公子

谢谢你的打赏!我将继续努力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值