【发布时间】:2020-06-28 21:31:11
【问题描述】:
我正在使用 ncurses 尝试一些 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,但这不起作用。那么我错过了什么边框没有在第一个程序中显示?
【问题讨论】:
-
关于;
printf("not enough arguments\n");1) 错误消息应该输出到stderr,而不是stdout。 2) 此错误消息未能告诉用户(和我们)参数列表应该是什么。建议以fprintf( stderr, "USAGE: %s <list of arguments, separated by spaces>\n". argv[0] );开头 -
参数应该是高度、宽度、y和x。我很确定这很明显。我没有写正确的错误消息,因为我只是在测试。但是感谢您向我展示了报告错误的正确方法。之前知道
stderr,但不知道如何正确使用。