重写ncurses

时间:2010-03-20 05:48:41

标签: c ncurses

我正在尝试重绘一个简单循环的内容。到目前为止,它打印到stdscr 100行,然后使用scrl滚动n行,我得到n个空白行。

我想要的是保持序列。但是,我不确定如何用额外的n行重绘stdscr。任何想法都会很感激!

#include <ncurses.h>
int main(void)
{
    initscr();
    int maxy,maxx,y;
    getmaxyx(stdscr,maxy,maxx);
    scrollok(stdscr,TRUE);
    for(y=0;y<=100;y++)
        mvprintw(y,0,"This is some text written to line %d.",y);
    refresh();
    getch();
    scrl(5);
    refresh();
    getch();
    endwin();
    return(0);
}

1 个答案:

答案 0 :(得分:0)

据推测,在refresh()之后的scrl()之后,添加以下行:

for (y = 96; y <= 100; y++)
    mvprintw(y, 0, "This is some text written as line %d.", y+5);
refresh();

当然,我正在使用魔术数字。你应该使用类似的东西:

enum { MAX_SCR_LINE = 100 };
enum { MAX_SCROLL   =   5 };

然后循环是:

for (y = MAX_SCR_LINE - MAX_SCROLL + 1; y <= MAX_SCR_LINE; y++)
    mvprintw(y, 0, "This is some text written as line %d.", y + MAX_SCROLL);
refresh();

如果您需要重复此过程,则计算结果为:

enum { MAX_ITER = 10 };
for (int j = 0; j < MAX_ITER; j++)
{
    scrl(MAX_SCROLL);
    for (y = MAX_SCR_LINE - MAX_SCROLL + 1; y <= MAX_SCR_LINE; y++)
        mvprintw(y, 0, "This is the text written as line %d.",
                 y + (j + 1) * MAX_SCROLL);
 }

...诸如此类

(代码未经测试 - 当心错误。)