以下内容摘自 beginning linux programming 一书
编译时加上 -lncurses选项
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <curses.h>
int main()
{
int i;
initscr();
if(!has_colors())
{
endwin();
fprintf(stderr,"error - no color support on this terminal\n");
exit(1);
}
if(start_color() != OK)
{
endwin();
fprintf(stderr,"error - could not initialize colors\n");
exit(2);
}
clear();
mvprintw(5,5,"there are %d colors,and %d color_pairs available",COLORS,COLOR_PAIRS);
refresh();
init_pair(1,COLOR_RED,COLOR_BLACK);
init_pair(2,COLOR_RED,COLOR_GREEN);
init_pair(3,COLOR_GREEN,COLOR_RED);
for(i=1;i<=3;++i)
{
attroff(A_BOLD);
attrset(COLOR_PAIR(i));
mvprintw(5+i,5,"color pair %d",i);
attrset(COLOR_PAIR(i)|A_BOLD);
mvprintw(5+i,25,"bold color pair %d",i);
refresh();
sleep(1);
}
endwin();
exit(0);
}