简体   繁体   中英

How to avoid stdscr overlapping in ncurses?

In my application I have two WINDOW objects, which bisects the terminal window, like a split-screen. But when I'm using wprintw() I can't see any output on the screen. I'm sure, that stdscr overlaps these two windows. How can I avoid this overlapping? Maybe I need to use wrefresh() or refresh() ? I've tried, but it doesn't help.
Here is the simplified part of my code. Maybe I'm doing something wrong?

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();

stdscr by definition covers the screen, so it will always overlap with any other window you create. The solution is to avoid using stdscr if you want to have multiple windows.

But the place where you're referencing stdscr is perhaps not obvious -- it's in the call to getch() , which can also be read as wgetch(stdscr) . This does an implicit wrefresh(stdscr) . which overwrites the screen with the (blank) contents of stdscr .

You can avoid this problem by changing the getch() call to wgetch(win1) or wgetch(win2) . In this example, it doesn't matter which window you choose; if you were displaying the input, you'd want to use the window where the input should appear.

Alternatively, you could call refresh() right at the start of the program, before refreshing win1 or win2 . Then, as long as you never wrote anything to stdscr , you could safely use getch() as much as you liked, since the implicit refresh() would find nothing updated in the window to display.

Sorry guys for wasting your time! I found an answer by myself! Here is the code:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM