如何避免 ncurses 中的 stdscr 重叠?

How to avoid stdscr overlapping in ncurses?

在我的应用程序中,我有两个 WINDOW 对象,它们将终端 window 一分为二,就像一个分屏。但是当我使用 wprintw() 我在屏幕上看不到任何输出。我敢肯定,stdscr 与这两个 windows 重叠。我怎样才能避免这种重叠? 也许我需要使用 wrefresh()refresh()?我试过了,但没有用。
这是我的代码的简化部分。也许我做错了什么?

WINDOW *win1 = newwin(10, width, 0, 0);
WINDOW *win2 = newwin(10, width, width, 0);

wprintw(win1, "First window: ");
wprintw(win2, "Second window: ");

wrefresh(win1);
wrefresh(win2);

while((ch = getch()) != KEY_F(2)) {}

endwin();

抱歉浪费你们的时间!我自己找到了答案! 这是代码:

WINDOW *win1, *win2;
int maxx, maxy, halfx;

getmaxyx(stdscr, maxy, maxx);
halfx = maxx >> 1;

win1 = newwin(maxy, halfx, 0, 0);
wgetch(win1, "First window");
wrefresh(win1);

win2 = newwin(maxy, halfx, 0, halfx);
wgetch(win2, "Second window");
wrefresh(win2);

stdscr 根据定义覆盖屏幕,因此它总是会与您创建的任何其他 window 重叠。解决方案是避免使用 stdscr 如果你想有多个 windows.

但是您引用 stdscr 的地方可能并不明显——它在对 getch() 的调用中,也可以读作 wgetch(stdscr)。这会隐式 wrefresh(stdscr)。用 stdscr.

的(空白)内容覆盖屏幕

您可以通过将 getch() 调用更改为 wgetch(win1)wgetch(win2) 来避免此问题。在此示例中,选择哪个 window 并不重要;如果要显示输入,则需要在应显示输入的位置使用 window。

或者,您可以在程序开始时调用 refresh(),然后再刷新 win1win2。然后,只要您从未向 stdscr 写入任何内容,您就可以随心所欲地安全地使用 getch(),因为隐式 refresh() 不会在 window 中找到任何更新显示。