如何使用ncurses实现滚动?

时间:2017-02-24 18:25:04

标签: c++ scroll command-line-interface ncurses

关于如何在ncurses中滚动有很多问题,但我没有找到任何明确的答案。所以,这是我的情况。我有两个 WINDOW * 对象,它们将屏幕分成两部分,就像分屏一样。现在我正在实现我的应用程序的一部分,就像文件管理器一样。第一个窗口包含文件名,第二个窗口包含当前目录中文件的扩展名。我想让它可滚动,我知道,我可以用 pad 来实现它。我能找到的唯一线索是this。这是我尝试使用这种方法:

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

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

win1 = newpad(maxy, halfx);
wprintw(win1, "File name: \n");
wrefresh(win1);

//don't pay much attention on this part of
//code, now it's just a window, then it will be pad too
win2 = newwin(maxy, halfx, 0, halfx);
wprintw(win2, "Extension: \n");
wrefresh(win2);

curs_set(0);//cursor off
int rowcount = 0;

//filling the windows with file data
for(directory_iterator beg(dir); beg != directory_iterator{}; ++beg)
{
    wprintw(win1, "%s\n", beg->path().stem().string().c_str());
    wrefresh(win1);
    wprintw(win2, "%s\n", beg->path().extension().string().c_str());
    wrefresh(win2);

    rowcount++;
}

//##############################
keypad(win1, TRUE);//SOLUTION
//##############################

int mypadpos = 0;
prefresh(win1, mypadpos, 0, 0, 0, maxy, maxx);

//spaghetti-code from the above question that I tried to adapt 
while ((ch = wgetch(win1)) != 'q')
{
    switch (ch)
    {
        case KEY_UP:
            if (mypadpos >= 0)
                mypadpos--;

            prefresh(win1, mypadpos, 0, 0, 0, maxy, maxx);
            break;

        case KEY_DOWN:
            if (mypadpos <= rowcount+1)
                mypadpos++;

            prefresh(win1, mypadpos, 0, 0, 0, maxy-1, maxx);
            break;
    }
}

问题是代码的最后一部分根本不起作用。

1 个答案:

答案 0 :(得分:0)

啊,答案很简单。我忘记了键盘()功能。对于那些试图在ncurses中滚动的人来说,这是正确的解决方案。