【zz】如何在 Cmd DOS命令列显示彩色文字(使用Windows API)

http://genewince.blogspot.com/2008/04/cmd.html

#include<windows.h> //GetStdHandle和SetConsoleTextAttribute在頭文件windows.h中 #include<iostream> using namespace std; void SetColor(unsigned short ForeColor=4,unsigned short BackGroundColor=0) { HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); //本例以輸出為例   SetConsoleTextAttribute(hCon,ForeColor|BackGroundColor); };int main() { SetColor(0xE ,0x0); std::cout<<"Hello world!"<<endl; SetColor(0xF ,0x0); std::cout<<"Hello world!"<<endl; return 0; }

c:program filesmicrosoft visual studio 8vcplatformsdkincludewincon.h
#define FOREGROUND_BLUE      0x0001 // text color contains blue.
#define FOREGROUND_GREEN     0x0002 // text color contains green.
#define FOREGROUND_RED       0x0004 // text color contains red.
#define FOREGROUND_INTENSITY 0x0008 // text color is intensified.
#define BACKGROUND_BLUE      0x0010 // background color contains blue.
#define BACKGROUND_GREEN     0x0020 // background color contains green.
#define BACKGROUND_RED       0x0040 // background color contains red.
#define BACKGROUND_INTENSITY 0x0080 // background color is intensified.

so…. you can define more colors by following the example code

const int BLACK = 0x0000;
const int BLUE = 0x0001 | 0x0008;
const int GREEN = 0x0002 | 0x0008;
const int CYAN = 0x0003 | 0x0008;
const int RED = 0x0004 | 0x0008;
const int MAGENTA = 0x0005 | 0x0008;
const int YELLOW = 0x0006 | 0x0008; 黃色=紅+綠
const int WHITE = 0x0007 | 0x0008;

~~ end ~~