注:
安装命令:sudo apt-get install libncurses5-dev
为了能够使用Ncurses库,必须在源程序中将#include<curses.h>包括进来,而且在编译的需要与它链接起来.
在gcc中可以使用参数-lncurses进行编译.
-
initscr(void);
是curses模式的入口。将终端屏幕初始化为curses模式,为当前屏幕和相关的数据结构分配内存。 -
int endwin(void);
是curses模式的出口,退出curses模式,释放curses子系统和相关数据结构占用的内存。 -
int curs_set(int visibility);
设置光标是否可见,visibility:0(不可见),1(可见) -
int move(int new_y, int new_x);
将光标移动到new_y所指定的行和new_x所指定的列 -
int addch(const chtype char);
在当前光标位置添加字符 -
int addstr(const chtype char[]);
在当前光标位置添加字符串 -
int refresh(void);
刷新物理屏幕。将获取的内容显示到显示器上。 -
int keypad(WINDOW *window_ptr, bool key_on);
允许使用功能键。exp:keypad(stdscr,1);//允许使用功能按键 -
int getch(void);
读取键盘输入的一个字符 -
chtype inch(void);
获取当前光标位置的字符。
注:curses有自己的字符类型chtype,使用时强制类型转换为char -
int start_color(void);
启动color机制,初始化当前终端支持的所有颜色 -
int init_pair(short pair_number, short foreground, short background);
配置颜色对
COLOR_BLACK 黑色 COLOR_MAGENTA 品红色
COLOR_RED 红色 COLOR_CYAN 青色
COLOR_GREEN 绿色 COLOR_WHITE 白色
COLOR_YELLOW 黄色 COLOR_BLUE 蓝色 -
int COLOR_PAIR(int pair_number);
设置颜色属性,设置完颜色对,可以通过COLOR_PAIR实现 -
int attron(chtype attribute);
启用属性设置 -
int attroff(chtype attribute);
关闭属性设置
例子:
#include <stdio.h>
#include <curses.h>
int main (int argc, char *argv[]) {
char ch;
initscr();//进入curses模式
curs_set(0);//光标可见(cursor visibility) 0不可见, 1可见
//noecho(); //禁止显示输入的字符
keypad(stdscr, 1);//允许使用功能键
start_color();//启动颜色机制
init_pair(1, COLOR_WHITE, COLOR_RED);
init_pair(2, COLOR_GREEN, COLOR_YELLOW);
ch = getch();
if(ch == 'Q') {
attron(COLOR_PAIR(1));//添加颜色
move(10, 10);//10行10列位置添加字符‘A’
addch('A');
refresh();//刷新
attroff(COLOR_PAIR(1));//关闭属性设置
}
ch = (char) inch();//获取当前光标的字符
if(ch == 'A') {
attron(COLOR_PAIR(2));
move(10, 10);
addch('C');
refresh();
attroff(COLOR_PAIR(2));
}
while(1);
endwin();//退出curses模式
}
结果: