我使用ncurses库用C语言编程(这是第一次),我有两个问题。我在ubuntu上使用默认终端(gnome终端)。
1)我需要调整终端的大小。我使用了resizeter()和resize_term(),但它们都失败了。
2)我使用scrollok()函数,问题是我丢失了滚动的行(当我使用wscrl()返回时,有空行)。
#include <ncurses.h>
int main() {
WINDOW *win, *win2;
int i;
char c;
initscr();
cbreak();
noecho();
win=newwin(8,20,1,1);
box(win,0,0);
win2=newwin(6,18,2,2);
scrollok(win2,1);
wrefresh(win);
wrefresh(win);
for(i=0;i<15;i++){
c=wgetch(win2);
if(c=='u'){
wscrl(win2,-1);
wrefresh(win2);
}
else{
wprintw(win2,"%c\n",c);
wrefresh(win2);
}
}
delwin(win);
delwin(win2);
endwin();
return 0;
}
发布于 2011-01-20 02:22:52
SIGWINCH
信号并在处理程序中调用resizeterm
(使用鼠标,probably).如果n为正,则向上滚动stdscr
。从stdscr
的顶部开始丢失n行,并在底部插入n个空行。如果n为负数,则向下滚动stdscr
。在stdscr
的顶部插入n个空行,从底部开始丢失n行。
因此,您必须手动保存输入内容,并在滚动时重新打印。
发布于 2015-09-28 17:12:06
POSIX不涉及这种情况,因为curses文档不是POSIX的一部分。Open Group碰巧维护了这两个项目的文档:
signal.h
(请注意,SIGWINCH
不存在)正如resizeterm
手册页中所指出的,您不应该从信号处理程序中调用该函数,因为它调用“不安全”函数。在几个地方讨论了“不安全”函数的主题;在gcc的documentation中,这将是一个开始。
关于文档,@larsman似乎引用了scroll(3)
的内容,但没有引用ncurses和"POSIX“的可比链接。值得一提的是:
回到OP的问题:
resizeterm
和resize_term
的使用。虽然没有说明,但可能是OP调整了终端窗口的大小,程序没有响应。resizeterm
的手册页面足够清晰,ncurses不会导致终端调整大小。为此(在某些终端上),可以使用resize
(一个用于xterm
的实用程序)的-s
选项。如果成功,则调整终端的大小,然后终端发送SIGWINCH
。ncurses为此提供了一个预定义的信号处理程序,但在应用程序级别,建议使用处理KEY_RESIZE
的方式。ncurses-examples中有几个程序可以做到这一点。发布于 2012-05-30 15:57:54
不能从ncurses调整终端窗口的大小,但可以调整resize系统调用的终端的大小。
#include <ncurses.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
WINDOW *ventana1;
system("resize -s 30 80");
initscr();
start_color();
ventana1 = newwin(15, 50, 0, 0);
init_pair(1,COLOR_YELLOW,COLOR_BLUE);
init_pair(2,COLOR_BLUE, COLOR_YELLOW);
wbkgd(ventana1,COLOR_PAIR(1));
wprintw(ventana1, "POLLO");
wrefresh(ventana1);
wgetch(ventana1);
wgetch(ventana1);
system("resize -s 20 60");
wbkgd(ventana1,COLOR_PAIR(2));
wprintw(ventana1, "POLLO");
wrefresh(ventana1);
wgetch(ventana1);
wgetch(ventana1);
system("resize -s 35 85");
system("clear");
wbkgd(ventana1,COLOR_PAIR(1));
wprintw(ventana1, "POLLO");
wrefresh(ventana1);
wgetch(ventana1);
wgetch(ventana1);
delwin(ventana1);
endwin();
system("resize -s 25 75");
}
https://stackoverflow.com/questions/4738803
复制相似问题