[英]How to scroll a window (other than stdscreen) in ncurses?
我看到這個答案正在研究解決我的問題https://stackoverflow.com/a/8407120/2570513 ,但是,它僅適用於stdscreen。 我實現了這個:
#include <ncurses.h>
int main(void)
{
int i = 2, height, width;
WINDOW *new;
initscr();
getmaxyx(stdscr, height, width);
new = newwin(height - 2, width - 2, 1, 1);
scrollok(new,TRUE);
while(1)
{
mvwprintw(new, i, 2, "%d - lots and lots of lines flowing down the terminal", i);
++i;
wrefresh(new);
}
endwin();
return 0;
}
但它不會滾動。 怎么了?
這是因為你使用mvwprintw
將字符串放在窗口中的某個位置,所以當i
比windowsize更大時,它就不會打印在屏幕上。
為了使用scolling,你需要使用wprintw
將文本放在當前光標位置。
#include <ncurses.h>
int main(void)
{
int i = 2, height, width;
WINDOW *new;
initscr();
getmaxyx(stdscr, height, width);
new = newwin(height - 2, width - 2, 1, 1);
scrollok(new,TRUE);
while(1)
{
wprintw(new, "%d - lots and lots of lines flowing down the terminal\n", i);
++i;
wrefresh(new);
}
endwin();
return 0;
}
如果要填充包含內容的窗口,然后使用箭頭鍵向上和向下滾動,則應該查看Pads
mvprintw
函數首先嘗試將光標移動到指示的位置,例如,使用wmove
。 wmove
函數永遠不會導致滾動,並且嘗試將其移動到窗口的底線失敗(引自wmove
手冊):
這些例程在失敗時返回ERR並且在成功完成后確定(SVr4僅指定“除ERR之外的整數值”)。
具體來說,如果窗口指針為空,或者位置在窗口之外 ,它們將返回錯誤。
相反,要進行滾動,您必須在窗口底部用換行符 (即'\\n'
)編寫文本。 wprintw
很有用; 反過來它調用waddch
(引用后者的手冊):
addch , waddch , mvaddch和mvwaddch例程將字符ch放在當前窗口位置的給定窗口中,然后進行提升。 它們類似於stdio中的putchar (3)。 如果預付款位於合適的邊距:
...
在當前滾動區域的底部,如果啟用了scrollok ,則滾動區域向上滾動一行。
如果ch是制表符,換行符或退格鍵,則光標會在窗口中正確移動:
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.