使用c ++在ncurses中打印子菜单的问题

问题描述 投票:1回答:2

我正在尝试打印与ncurses显示的主菜单相关联的子菜单。这就是我组织它的方式:

  1. do {} while();使用wgetch循环获取用户的键盘输入
  2. 如果用户按下回车键,清除整个屏幕后会显示subMenu条目。

不幸的是,我无法通过第二步,子菜单永远不会出现在屏幕上。

#include <ncurses.h>
#include <iostream>
#include <string>

int main()
{ 
    std::string nameMainMenuExample = "/parent1/folder";

    std::string nameSubMenuExample = "/folder/file";

    // initialize ncurses
    WINDOW *win;
    win = initscr();
    raw();
    curs_set(0);
    cbreak();
    box(win, 0, 0); 
    refresh();
    wrefresh(win);
    keypad(win, true);
    // end initialize ncurses

    int highlight = 0;
    int choice;

    // PRESS 'a' to ESCAPE LOOP
    do {
        mvwprintw(win, 1, 1, nameMainMenuExample.c_str());
        switch (choice) {
            case KEY_UP:
                --highlight;
                if (highlight == -1) {
                    highlight = 0;
                }
                break;
            case KEY_DOWN:
                ++highlight;
                if (highlight == 1) {
                    highlight = 0;
                }
                break;
            case KEY_ENTER:                        // Enter key pressed
                clear();
                mvwprintw(win, 1, 1, nameSubMenuExample.c_str());
                refresh();
                break;
            default:
                break;
        }
    } while ((choice = wgetch(win)) != 97); // random choice a == 97

    endwin();
    return 0;
}

我只是希望在ncurses清除主菜单的屏幕后在屏幕上打印子菜单。谢谢

c++ ncurses
2个回答
0
投票

如果你想激活enter键上的子菜单,你应该检查wgetch返回的值KEY_ENTER(数字上类似于16777221),而不是10。


0
投票

你将调用混合到不同的窗口(clearrefresh使用stdscr),你的wgetch调用使用的是获得自己的wrefresh。由于菜单窗口没有被刷新,它永远不会出现,并且由于wgetch做了wrefresh,这可能另外模糊的东西。

首先将wrefresh调用应用于您想要重新绘制的窗口。

© www.soinside.com 2019 - 2024. All rights reserved.