我正在使用 ncurses 制作一个程序,将屏幕分成两个窗口。顶部屏幕可以接受输入,通过按“#”,它将把所有文本向下移动到底部窗口并删除顶部窗口。在我的代码中,我尝试使用 copywin() 替换底部窗口,但它不会将文字粘贴到第二个窗口中。这就是我所拥有的...
#include <ncurses.h>
int main(int argc, char *argv[])
{
// Declare variables for windows and sizes
WINDOW *top_win, *bottom_win;
int maxx, maxy, halfy, flag = 0, ch;
// Start curses
initscr();
noecho();
refresh();
// get the max x's and y's
getmaxyx(stdscr, maxy, maxx);
halfy = maxy >> 1;
// Start color
start_color();
init_pair(1, COLOR_BLACK, COLOR_WHITE);
init_pair(2, COLOR_WHITE, COLOR_CYAN);
init_pair(3, COLOR_RED, COLOR_WHITE);
// Make windows
top_win = newwin(halfy, maxx, 0, 0);
wbkgd(top_win, COLOR_PAIR(1));
wrefresh(top_win);
bottom_win = newwin(halfy, maxx, halfy, 0);
wbkgd(bottom_win, COLOR_PAIR(2));
wrefresh(bottom_win);
// Allow functions keys
keypad(top_win, TRUE);
keypad(bottom_win, TRUE);
// while loop to get input
while((ch = getch()) != '`')
{
if(ch == '@')
{
if(flag == 1)
{
flag = 0;
}
else
{
flag = 1;
}
}
else if(ch == '#')
{
//waddstr(bottom_win, "testing");
copywin(top_win, bottom_win, 0, 0, halfy, 0, halfy, maxx, TRUE);
//overwrite(top_win, bottom_win);
//werase(top_win);
}
else if(flag != 1)
{
waddch(top_win, ch | COLOR_PAIR(1));
}
else if(flag == 1)
{
waddch(top_win, ch | COLOR_PAIR(3));
}
wrefresh(top_win);
wrefresh(bottom_win);
}
// end curses
delwin(top_win);
delwin(bottom_win);
endwin();
return 0;
}
我知道我可以使用“#”字符打印到窗口,因为我注释掉了测试语句。我也刚刚尝试过使用 overwrite(),但这也不起作用。我只是把论点混淆了,还是另有原因?有任何想法吗?预先感谢您!
最佳答案
copywin
检查给定的行/列,并且 decides that your destination rectangle doesn't lie completely within the destination window 。以下是对您的程序的快速修复:
--- foo.c.orig 2017-02-13 16:13:12.000000000 -0500
+++ foo.c 2017-02-13 16:30:18.037987489 -0500
@@ -51,7 +51,7 @@
else if(ch == '#')
{
//waddstr(bottom_win, "testing");
- copywin(top_win, bottom_win, 0, 0, halfy, 0, halfy, maxx, TRUE);
+ copywin(top_win, bottom_win, 0, 0, 0, 0, halfy - 1, maxx - 1, TRUE);
//overwrite(top_win, bottom_win);
//werase(top_win);
}
@@ -73,4 +73,3 @@
endwin();
return 0;
}
行和列从零开始编号到最后一行/列(比窗口大小小一),因此我从 dmaxrow 和 dmaxcol 中减去一参数。第五个参数dminrow超出了窗口的底部。
ncurses 检查参数。关于兼容性和可移植性,使用 Solariscurses 运行相同的程序(将“ncurses.h”更改为“curses.h”)会转储核心。
手册页还可以改进,但是关于 colors 已经足够清楚了:
only text where the two windows overlap is copied
关于c - 覆盖窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42195034/