c++ - 诅咒库 : why does getch() clear my screen?

标签 c++ curses getch pdcurses

我正在尝试使用 C++ 学习 curses 库(pdcurses,因为我在 Windows 操作系统中)。 我有一个显示 3 个窗口的程序,然后是一个 while 循环来根据 getch() 捕获的按键进行一些处理。当按下 F1 键时,循环退出。

然而,尽管使用 wrefresh() 刷新了所有三个窗口,但在我输入第一次按键之前没有任何显示。没有 while 循环,一切都显示正常。我做了很多测试,好像第一次调用 getch() 会完全清除屏幕,但后续的不会。

我的问题是:我错过了什么?起初,我想也许 getch() 正在调用一个隐式的 refresh(),但为什么后续调用它的行为不同?

非常感谢您的帮助。

这是代码。

#include <curses.h>

int main()
{
    initscr();
    raw();
    keypad(stdscr, TRUE);
    noecho();
    curs_set(0);

    WINDOW *wmap, *wlog, *wlegend;
    int pressed_key;
    int map_cursor_y = 10, map_cursor_x = 32;

    wlog = newwin(5, 65, 0, 15);
    wlegend = newwin(25, 15, 0, 0);
    wmap = newwin(20, 65, 5, 15);

    box(wmap, 0 , 0);
    box(wlog, 0 , 0);
    box(wlegend, 0 , 0);

    mvwprintw(wlog, 1, 1, "this is the log window");
    mvwprintw(wlegend, 1, 1, "legends");
    mvwaddch(wmap, map_cursor_y, map_cursor_x, '@');

    wrefresh(wlog);
    wrefresh(wmap);
    wrefresh(wlegend);

    while ((pressed_key = getch()) != KEY_F(1))
    {
         /* process keys to move the @ cursor (left out because irrelevant) */

         box(wmap, 0 , 0);
         box(wlog, 0 , 0);
         box(wlegend, 0 , 0);
         wrefresh(wmap);
         wrefresh(wlog);
         wrefresh(wlegend);
    }

    endwin();
    return 0;
}

最佳答案

您的第一直觉是正确的:getch() 执行隐式 refresh()。具体来说,getch() 等同于 wgetch(stdscr),因此它是一个隐式的 wrefresh(stdscr) —— 更新一个窗口(stdscr),你不会以其他方式使用它,它恰好填满了屏幕。从那时起后续调用没有影响的原因是 stdscr 已经是最新的,就 curses 而言,因为在那之后你再也不会写它了(不要介意它的内容已在实际屏幕上被覆盖)。

解决方案是在开始绘制之前在顶部显式调用 refresh();或者,我更喜欢在不同的窗口(以最合适的为准)调用 wgetch(),而不是 getch(),并忽略 stdscr 的存在 完全。请记住,所有不允许您指定窗口的函数——getch()refresh() 等——实际上是调用它们的“w "等效项,使用 stdscr 作为隐式窗口参数。

关于c++ - 诅咒库 : why does getch() clear my screen?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19748685/

相关文章:

c++ - std 列表错误

c++ - std::istream 的运算符类型无效?

perl - 如何使用 Perl 和 Curses 指定透明背景?

c - Strcat 在类似 getch 的简单密码输入上抛出段错误

c - 使用 getch() 重复 key 检查的问题

c++ - 禁止使用模板化的虚拟成员函数,有替代方法吗?

c++ - 我将如何着手将天空盒应用于世界,openGL C++

c - 如何在结构中保存 ncurses 窗口?

c++ - 如何使用 NCurses 阻止 C++ 中的某些键

c - Twitter 之类的 "characters left"C 中的函数问题 : How to prevent it from going to the next line?