在 ANSI 兼容终端(例如 xterm、rxvt、konsole 等)里, 可以用彩色显示文本而不仅仅是黑白。
但是我们自己编写的程序能否输出彩色的字符呢?当然答案是肯定的。下面的语句就输出高亮的黑
色背景的绿色字。
printf("\033[1;40;32m%s\033[0m",” Hello,NSFocus\n”);
\033 声明了转义序列的开始,然后是 [ 开始定义颜色。后面的 1 定义了高亮显示字符。然后是背
景颜色,这里面是40,表示黑色背景。接着是前景颜色,这里面是32,表示绿色。我们用
\033[0m 关闭转义序列, \033[0m 是终端默认颜色。
通过上面的介绍,就知道了如何输出彩色字符了。因此,我就不再多说了。下面是对于彩色字符颜
色的一些定义:
前景 背景 颜色
---------------------------------------
30 40 黑色
31 41 紅色
32 42 綠色
33 43 黃色
34 44 藍色
35 45 紫紅色
36 46 青藍色
37 47 白色
代码 意义
-------------------------
0 终端默认设置
1 高亮显示
4 使用下划线
5 闪烁
7 反白显示
8 不可见
下面给出了一个C语言的示例:
///////////////////////////////////////////////////////////////////////////////
//filename:tracelog.c
#include <stdio.h>
int main(int argc,char **argv)
{
unsigned char attr[]={0,1,4,5,7,8};
unsigned char fore[]={30,31,32,33,34,35,36,37};
unsigned char back[]={40,41,42,43,44,45,46,47};
int adx,fdx,bdx;
for(bdx=0;bdx <sizeof(back);bdx++)
{
for(fdx=0;fdx <sizeof(fore);fdx++)
{
for(adx=0;adx <sizeof(attr);adx++)
{
printf("\033[%d;%d;%dmhello,NSFocus!!!\033[0m",
attr[adx],fore[fdx],back[bdx]);
printf("<==attr=%d,fore=%d,back=%d\n",
attr[adx],fore[fdx],back[bdx]);
}
}
printf("\n");
}
return 0;
}
C/C++彩色输出的办法
-
- 帖子: 259
- 注册时间: 2008-11-14 20:35
- ask_me
- 帖子: 374
- 注册时间: 2009-03-22 9:54
Re: C/C++彩色输出的办法
看标题还以为是实现类似colorgcc的功能呢,原来不是……
-
- 帖子: 259
- 注册时间: 2008-11-14 20:35
Re: C/C++彩色输出的办法
百度google才知colorgcc是什么了。谢谢啦,这个也有用。ask_me 写了:看标题还以为是实现类似colorgcc的功能呢,原来不是……
- CGer
- 帖子: 210
- 注册时间: 2009-06-21 19:38
- 来自: CUMT
Re: C/C++彩色输出的办法
Mark
代码: 全选
sudo apt-get install girlfriend
正在读取软件包列表... 完成
正在分析软件包的依赖关系树... 完成
有一些软件包无法被安装。
下列的信息可能会对解决问题有所帮助:
下列的软件包有不能满足的依赖关系:
girlfriend: 依赖: house但是它将不会被安装
girlfriend: 依赖: car但是它将不会被安装
house,car: 依赖: money但是它将不会被安装
E: 无法安装的软件包
-
- 帖子: 60
- 注册时间: 2006-05-01 12:29
- 来自: zju
Re: C/C++彩色输出的办法
这是用perl码的吧ask_me 写了:看标题还以为是实现类似colorgcc的功能呢,原来不是……
- nchacker
- 帖子: 7
- 注册时间: 2009-08-21 18:06
- 来自: 湖南科技大学
- 联系:
Re: C/C++彩色输出的办法
这个还是满有用的~~呵呵。 收藏下~!
Please call me E. I'm glad to make friends with you!
http://blog.csdn.net/nchacker/ - http://hi.baidu.com/caieezone/ - http://renren.com/profile.do?id=232600803
http://blog.csdn.net/nchacker/ - http://hi.baidu.com/caieezone/ - http://renren.com/profile.do?id=232600803
- xiaocheng_zh
- 帖子: 46
- 注册时间: 2009-05-30 15:52
- 来自: DL LIAONING CHN
Re: C/C++彩色输出的办法
哪儿有c++的代码啊??????
-
- 帖子: 259
- 注册时间: 2008-11-14 20:35
Re: C/C++彩色输出的办法
给你看一下我为我的一个程序写的帮助函数吧,把它改成了main函数,你可以自己去编译,在Linux下可用。xiaocheng_zh 写了:哪儿有c++的代码啊??????
代码: 全选
/*
* =====================================================================================
*
* Filename: color.h
*
* Description:
*
* Version: 1.0
* Created: 2009年08月17日 19时29分06秒
* Revision:
* Compiler: gcc
*
* Author: Hu Chunlong (hchl), hchl@mail.ustc.edu.cn
* Company: University of Science and Technology of China
* Copyright: Copyright (c) 2009, Hu Chunlong
*
* =====================================================================================
*/
#define BLA "\033[0;30m"
#define RED "\033[0;31m"
#define GRE "\033[0;32m"
#define BRO "\033[0;33m"
#define BLU "\033[0;34m"
#define PUR "\033[0;35m"
#define CYA "\033[0;36m"
#define LGRA "\033[0;37m"
#define DGRA "\033[1;30m"
#define LRED "\033[1;31m"
#define LGRE "\033[1;32m"
#define YEL "\033[1;33m"
#define LBLU "\033[1;34m"
#define LPUR "\033[1;35m"
#define LCYA "\033[1;36m"
#define WHI "\033[1;37m"
#define DEF "\033[0m"
代码: 全选
/*
* =====================================================================================
*
* Filename: help.cpp
*
* Description:
*
* Version: 1.0
* Created: 2009年08月16日 09时15分24秒
* Revision:
* Compiler: gcc
*
* Author: Hu Chunlong (hchl), hchl@mail.ustc.edu.cn
* Company: University of Science and Technology of China
* Copyright: Copyright (c) 2009, Hu Chunlong
*
* =====================================================================================
*/
#include<iostream>
#include"color.h"
using namespace std;
int main(void)
{
cout << endl << RED << "\t\t\t\t\tMANUAL" << endl << endl << endl;
cout << GRE << "NAME" << endl;
cout << LBLU << "\tname - to be decided" << endl << endl;
cout << GRE << "SYNOPSIS" << endl;
cout << BLU << "\tname [-f " << YEL << "FilePath" << BLU << "] [-p " << YEL << "Period";
cout << BLU << "] [-vM " << YEL << "V_Max" << BLU << "] [-vm " << YEL << "V_Min" << BLU;
cout << BLU << "] [-h " << BLU << "]\n\t\t[-ui " << YEL << "Up_Interval";
cout << BLU << "] [-di " << YEL << "Down_Interval" << BLU << "] [-c0 " << YEL << "c0";
cout << BLU << "] [-c1 " << YEL << "c1" << BLU << "]\n\t\t[-c2 " << YEL << "c2" << BLU;
cout << "] [-c3 " << YEL << "c3" << BLU << "] [-a0 " << YEL << "a0" << BLU << "] [-a1 ";
cout << YEL << "a1" << BLU << "] [-a2 " << YEL << "a2" << BLU << "] [-a3 " << YEL;
cout << "a3" << BLU << "]\n\t\t[" << "-b0 " << YEL << "b0" << BLU << "] [-b1 " << YEL;
cout << "b1" << BLU << "] [-b2 " << YEL << "b2" << BLU << "] [-b3 " << YEL << "b3" << BLU;
cout << "] [-j " << YEL << "Jump" << BLU << "] [-d " << YEL << "Dead" << BLU << "]\n\n";
cout << GRE << "DESCRIPTION" << endl;
cout << BLU << "\t-f " << LBLU << "FilePath: The filename of the data file you want ";
cout << LBLU << "to process. If \n\t\tthis program is in the same directory as the data ";
cout << LBLU << "file, you \n\t\tcan just input the filename of the data file, or you ";
cout << LBLU << "have to \n\t\tinput the pathname of the data file, both full pathname ";
cout << LBLU << "and \n\t\trelative pathname is OK." << endl;
cout << BLU << "\t-p " << LBLU << "Period: " << endl;
cout << BLU << "\t-vM " << LBLU << "V_Max: The maximum of V." << endl;
cout << BLU << "\t-vm " << LBLU << "V_Min: The minimum of V." << endl;
cout << BLU << "\t-h " << LBLU << "Dispaly help information." << endl;
cout << BLU << "\t-ui " << LBLU << "Up_Interval: The interval of V ";
cout << LBLU << "when V is going up." << endl;
cout << BLU << "\t-di " << LBLU << "Down_Interval: The interval of V ";
cout << LBLU << "when V is going down" << endl;
cout << BLU << "\t-c0 " << LBLU << "c0: " << endl;
cout << BLU << "\t-c1 " << LBLU << "c1: " << endl;
cout << BLU << "\t-c2 " << LBLU << "c2: " << endl;
cout << BLU << "\t-c3 " << LBLU << "c3: " << endl;
cout << BLU << "\t-a0 " << LBLU << "a0: " << endl;
cout << BLU << "\t-a1 " << LBLU << "a1: " << endl;
cout << BLU << "\t-a2 " << LBLU << "a2: " << endl;
cout << BLU << "\t-a3 " << LBLU << "a3: " << endl;
cout << CYA << "Press ENTER to continue...";
cin.get();
cout << BLU << "\t-b0 " << LBLU << "b0: " << endl;
cout << BLU << "\t-b1 " << LBLU << "b1: " << endl;
cout << BLU << "\t-b2 " << LBLU << "b2: " << endl;
cout << BLU << "\t-b3 " << LBLU << "b3: " << endl;
cout << BLU << "\t-j " << LBLU << "Jump: Jump time." << endl;
cout << BLU << "\t-d " << LBLU << "Dead: Dead time." << endl;
cout << GRE << "AUTHOR" << endl;
cout << LBLU << "\tWritten by Hu Chunlong" << endl << endl;
cout << GRE << "REPORTING BUGS" << endl;
cout << LBLU << "\tReport bugs to <hchl@mail.ustc.edu.cn>." << endl;
cout << LBLU << "\tIf you want the source code ,you can also email me" << endl << endl;
cout << GRE << "COPYRIGHT" << endl;
cout << LBLU << "\tCopyright (c) 2009, Hu Chunlong." << endl << endl;
cout << GRE << "ACKNOWLEDGEMENTS" << endl;
cout << LBLU << "\tTo be completed" << endl << endl;
cout << CYA << "Press ENTER to continue...";
cin.get();
return 0;
}
上次由 dynamic0603 在 2009-08-26 22:19,总共编辑 2 次。
-
- 帖子: 259
- 注册时间: 2008-11-14 20:35
Re: C/C++彩色输出的办法
如果是在windows下的VC++,则可以把头文件color.h换成下面的:xiaocheng_zh 写了:哪儿有c++的代码啊??????
代码: 全选
#include<windows.h>
void SetColor(unsigned short ForeColor=4,unsigned short BackGroundColor=15)
{
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,ForeColor|BackGroundColor);
};
#define DEF "";SetColor();cout
#define RED "";SetColor(4,520);cout
#define GRE "";SetColor(10,0);cout
#define YEL "";SetColor(4,10);cout
#define BLU "";SetColor(265,0);cout
#define PUR "";SetColor(4,525);cout
#define CYA "";SetColor(515,0);cout
#define WHI "";SetColor(4,15);cout
#define LBLU "";SetColor(770,0);cout