//字符输入 #include <curses.h> main() { int ch; int x=5,y=5; initscr(); keypad(stdscr,TRUE); curs_set(0); noecho(); mvaddch(y,x,'A'); while(1) { ch=getch(); //mvaddch(y,x,' '); //clrtoeol(); erase(); //clear(); switch(ch) { case KEY_UP: y--; break; case KEY_DOWN: y++; break; case KEY_LEFT: x--; break; case KEY_RIGHT: x++; break; } mvaddch(y,x,'A'); refresh(); } endwin(); } //字符串输入 #include <curses.h> main() { char name[9]={0}; int r; initscr(); //绘制UI mvaddstr(4,10,"用户:[ ]"); //输入 r=mvgetnstr(4,16,name,8); //name[r]=0; //打印输入 mvprintw(7,10,"你输入的是:%s",name); refresh(); //输入字符 getch(); endwin(); } //格式输入 #include <curses.h> void init(); void drawUi(); void dealInput(); void destroy(); main() { init(); drawUi(); dealInput(); destroy(); } void dealInput() { int a,b; while(1) { mvaddstr(2,3," "); mvscanw(2,3,"%d",&a); mvaddstr(2,11," "); mvscanw(2,11,"%d",&b); mvaddstr(2,19," "); mvprintw(2,19,"%d",a+b); refresh(); } } void drawUi() { mvaddstr(2,2,"[ ]+[ ]=[ ]"); refresh(); } void destroy() { endwin(); } void init() { initscr(); }
gcc cur_getstr.c -o main -l curses
时间: 2024-09-23 02:14:23