linux-c-curses(1)-颜色+中文

本文介绍使用C语言和NCurses库进行文本界面颜色及显示属性设置的方法。通过实例演示了如何初始化颜色对、设置文本属性如高亮度、下划线等,并展示了完整的代码实现过程。

1、初始化颜色

//init_pair(short index,short foreground,short background)初始化颜色索引

//attron(COLOR_PAIR(索引号)|属性)

属性如下:

     A_NORMAL         Normal display (no highlight)
     A_STANDOUT       Best highlighting mode of the terminal.亮色
     A_UNDERLINE      Underlining
     A_REVERSE        Reverse video
     A_BLINK          Blinking闪烁
     A_DIM            Half bright半亮度
     A_BOLD           Extra bright or bold高亮度
     A_PROTECT        Protected mode
     A_INVIS          Invisible or blank mode
     A_ALTCHARSET     Alternate character set另类字符集
     A_CHARTEXT       Bit-mask to extract a character

2、编译:

1)必须安装cursesew库

sudo apt-get install libncursesw5-dbg libncursesw5-dev

2)编译

mysea@mysea-desktop:~/test$ gcc -lncursesw -std=c99 -o cursestest cursestest.c

cursestest.c: In function ‘main’:

cursestest.c:12: warning: implicit declaration of function ‘exit’

cursestest.c:12: warning: incompatible implicit declaration of built-in function ‘exit’

mysea@mysea-desktop:~/test$ ./cursestest

 



 3、代码

#include <ncursesw/ncurses.h>
#include <locale.h>
int main(void){
//init_pair(short index,short foreground,short background)初始化颜色索引
//attron(COLOR_PAIR(索引号)|属性)
    setlocale(LC_ALL,"");
    initscr();//初始化
    box(stdscr,ACS_VLINE,ACS_HLINE);//画边框
    if (!has_colors()||start_color()==ERR){
        endwin();
        printf("终端不支持颜色\n");
        exit(1);
    }
    init_pair(1,COLOR_GREEN,COLOR_BLACK);
    init_pair(2,COLOR_RED,COLOR_BLACK);
    init_pair(3,COLOR_WHITE,COLOR_BLUE);
    for (int i=1;i<=3;i++){
         attron(COLOR_PAIR(i));
         move(i,10);
         printw("deepfuture.iteye.com颜色:%d",i);
    }
    for (int i=1;i<=3;i++){
         attron(COLOR_PAIR(i)|A_UNDERLINE);
         move(i+5,10);
         printw("deepfuture.iteye.com颜色+下划线:%d",i);
    }
    refresh();//刷新屏幕
    getch();//等待按键
    endwin();//结束
    return 0;    
}











评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值