我正在试验一些使用ncurse的C++,在显示窗口边框时遇到了问题,但是下面的程序有问题。
#include <cstdio>
#include <cstdlib>
#include <ncurses.h>
int main(int argc, char **argv)
{
if(argc != 5)
{
printf("not enough arguments\n");
exit(1);
}
int height = atoi(argv[1]);
int width = atoi(argv[2]);
int y = atoi(argv[3]);
int x = atoi(argv[4]);
initscr();
WINDOW *win = newwin(height, width, y, x);
box(win, 0, 0);
wrefresh(win);
int py, px;
getparyx(win, py, px);
mvprintw(LINES-2, 0, "getparyx: (%d, %d)", py, px);
int by, bx;
getbegyx(win, by, bx);
mvprintw(LINES-1, 0, "getbegyx: (%d, %d)", by, bx);
getch();
delwin(win);
endwin();
}在上面的程序中,我使用box绘制边框,使用wrefresh刷新,但没有显示任何内容。不过,我在stdscr上打印的其他内容确实显示了这一点。
然而,在另一个程序中,我能够使边界正常工作。
#include <ncurses.h>
int main()
{
const int height = 6, width = 8;
WINDOW *win;
int starty, startx;
int ch;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
starty = (LINES - height) / 2;
startx = (COLS - width) / 2;
win = newwin(height, width, starty, startx);
box(win, 0, 0);
wrefresh(win);
while((ch = getch()) != KEY_F(1))
{
wborder(win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
wrefresh(win);
delwin(win);
switch(ch)
{
case KEY_UP:
win = newwin(height, width, --starty, startx);
break;
case KEY_DOWN:
win = newwin(height, width, ++starty, startx);
break;
case KEY_LEFT:
win = newwin(height, width, starty, --startx);
break;
case KEY_RIGHT:
win = newwin(height, width, starty, ++startx);
break;
}
move(starty + (height / 2) - 1, startx + (width / 2) - 1);
box(win, 0, 0);
wrefresh(win);
}
delwin(win);
endwin();
}问题是边框只出现在循环中。换句话说,直到我按下按钮,边框才开始显示,这意味着初始的wrefresh无法工作。
在做了一些研究之后,我的this线程建议在initscr之后(或者至少在wrefresh()之前)调用refresh,但这不起作用。那么,在第一个程序中没有显示边框,我错过了什么呢?
发布于 2020-06-28 22:03:20
对于我的测试,最初的refresh()肯定是缺少的。我检查了我编写的一些旧代码,它确实调用了refresh()作为ncurses初始化的一部分。将这个添加到您的代码中,使其对我有效。很多诅咒的文档仍然局限于书籍,它从来没有真正进入网络。
initscr();
refresh(); // <-- HERE
WINDOW *win = newwin( height, width, y, x );
box( win, 0, 0 );
wrefresh(win);我不认为窗口模型是完全初始化的,直到第一个refresh()被调用之后。但我找不到任何关于为什么会这样的文件。
所以这个答案没有太多细节,抱歉.但我希望这能帮上忙。
https://stackoverflow.com/questions/62628434
复制相似问题