yui*_*nnu 5 c terminal ncurses
我有一个ncurses程序,有多个子窗口作为列.每个子窗口都有固定的宽度和父终端窗口的高度.
然而,我发现如果终端的宽度减小,那么其中一个窗口会失去其固定宽度,并且似乎使用终端的整个剩余宽度"溢出"它们先前设置的边界.
这是我用于上面的代码:
#include <ncurses.h>
int main() {
WINDOW * winA, * winB;
int i = 0, width = 30;
initscr();
// Suppress stdout
noecho();
// Enable keypad
keypad(stdscr, true);
// interrupt, quit, suspend, and flow control characters are all passed through uninterpreted
raw();
winA = newwin(0, width, 0, 0);
winB = newwin(0, width, 0, width);
timeout(50);
while(getch() != 'q') {
i = width * getmaxy(stdscr);
werase(winA);
werase(winB);
while (i--) {
waddstr(winA, "0");
waddstr(winB, "1");
}
wnoutrefresh(stdscr);
wnoutrefresh(winA);
wnoutrefresh(winB);
doupdate();
}
endwin();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是另一个屏幕截图,显示了我实际程序中的问题.左侧的终端是正确的,右侧的终端显示调整窗口大小并触发此问题后的结果:
当终端调整到小宽度时,如何防止窗口丢失固定宽度?
您可以使用打击垫,而不是将窗户用于所有东西.Windows仅限于屏幕尺寸,而打击垫则不受限制.
当ncurses获得a时SIGWINCH,它会调整大小stdscr并包含其中的所有内容stdscr,根据需要缩小这些窗口以适应新的屏幕大小,或者(如果它们的边距与旧屏幕大小匹配),增加它们的大小.这是自动的.您的程序可以检查KEY_RESIZE返回的内容getch并调用wresize更改窗口大小(并重绘其内容).
如果您使用过垫,则不会调整大小(通过调用者可以调整的视口显示打击垫).