如何在 ncursesw 中使用和更新面板

how to use and update panels in ncursesw

我目前正在 Ubuntu WSL2 中使用 C++ 进行编码,使用 <panel.h> header 在 ncursesw 中创建面板,因为我需要 Unicode 字符。

我已经做了几个测试,结果显示在下面的 table 中:

这是使用 WINDOW* 和 -lncursesw 创建 window 时的代码和结果。

代码:

#include <panel.h>

int main() {
    setlocale(LC_ALL, "");
    initscr();
    noecho();
    cbreak();
    refresh();

    WINDOW *window {newwin(0, 0, 0, 0)};
    
    box(window, 0, 0);
    mvwaddstr(window, 1, 1, "\U0001F600");
    wrefresh(window);

    getch();
    endwin();
}

结果:

这是使用 PANEL* 和 -lncursesw 创建 window 时的代码和结果。

代码:

int main() {
    setlocale(LC_ALL, "");
    initscr();
    noecho();
    cbreak();

    PANEL *panel {new_panel(newwin(0, 0, 0, 0))};

    box(panel_window(panel), 0, 0);
    mvwaddstr(panel_window(panel), 1, 1, "\U0001F600");
    // wrefresh(panel_window(panel));
    update_panels();
    doupdate();

    getch();
    endwin();
}

结果:

但是,如果我取消注释 wrefresh() 行,我在使用 WINDOW * 时会得到相同的预期结果。为什么会这样?

doupdate不做wrefresh(一般用wnoutrefresh再准备一个windows)。

如果没有明确的 wrefresh,您的程序正在完成 wrefresh 作为 getch — which refreshes stdscr, but that in turn has only the pending werase from initscr followed by the box 调用的副作用:看起来您会得到一个空框。

我错误地使用了 -lpanel -lncursesw,而我本应使用 -lpanelw -lncursesw。这为我修好了。