我正在尝试使用 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/