使用 c++ 在 ncurses 中打印子菜单时出现问题

Issue with printing sub-menu in ncurses with c++

本文关键字:菜单 问题 打印 c++ ncurses 使用      更新时间:2023-10-16

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

  1. do{} while ((; 循环与 wgetch 从用户获取键盘输入
  2. 如果用户按回车键,则在清除整个屏幕后显示子菜单条目。

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

#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 清除主菜单的屏幕后将子菜单打印在屏幕上。谢谢

如果要激活 Enter 键上的子菜单,则应根据 KEY_ENTER(类似于数字16777221(检查返回的值wgetch,而不是 10。

您正在混合对不同窗口的调用(clearrefresh使用stdscr(,并且您的wgetch调用使用的调用正在获取自己的wrefresh。 由于菜单窗口没有刷新,它永远不会出现,并且由于wgetch执行wrefresh这可能会进一步模糊内容。

首先,使wrefresh调用应用于要重新绘制的窗口。

在C++中使用ncurses,ENTER 键值只是'n' 例如:

case 'n':
   clear();
   mvwprintw(win, 1, 1, nameSubMenuExample.c_str());
   refresh();
   break;