linux curses 彩色显示

来源:互联网 发布:中学生当街打母 知乎 编辑:程序博客网 时间:2024/05/17 04:37

以下内容摘自 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);

}

原创粉丝点击