在按下方向右键时,光标会先向右移动一位,
x----0 x----o xx----o xxxx----o
| | -> | | -> | | -> | |
x----o x----o X----o x----o
需要再次按下方向右键才会右移,但是左上角的字符会清理不掉,但是别的方向键没有这个现象,问题出在哪里呢 代码很简单:
#include <ncurses.h>
#include <locale.h>
WINDOW *create_new_window(int height, int width, int start_y, int start_x);
void destroy_window(WINDOW *win);
int main(int argc, char *argv[])
{
WINDOW *my_win;
int start_x, start_y, width, height;
int ch;
setlocale(LC_ALL, "");
initscr();
raw();
keypad(stdscr, TRUE);
height = 4;
width = 8;
start_y = (LINES - height) / 2;
start_x = (COLS - width) / 2 + 1;
printw("按下 F1 退出");
refresh();
my_win = create_new_window(height, width, start_y, start_x);
while((ch = getch()) != KEY_F(1))
{
switch (ch)
{
case KEY_UP:
destroy_window(my_win);
my_win = create_new_window(height, width, --start_y, start_x);
break;
case KEY_RIGHT:
destroy_window(my_win);
my_win = create_new_window(height, width, start_y, ++start_x);
break;
case KEY_DOWN:
destroy_window(my_win);
my_win = create_new_window(height, width, ++start_y, start_x);
break;
case KEY_LEFT:
destroy_window(my_win);
my_win = create_new_window(height, width, start_y, --start_x);
break;
}
}
endwin();
return 0;
}
WINDOW *create_new_window(int height, int width, int start_y, int start_x)
{
WINDOW *window;
window = newwin(height, width, start_y, start_x);
wborder(window, '|', '|', '-', '-', 'x', 'o', 'x', 'o');
wrefresh(window);
return window;
}
void destroy_window(WINDOW *window)
{
wborder(window, 1, 1, 1, 1, 1, 1, 1, 1);
wrefresh(window);
delwin(window);
}