windows 有提供一套conosle API來讀寫console緩存,字體、顏色、創建console等的, 但顏色格式沒有找到任意顏色的接口,只能用系統自定義幾種顏色。不過在命令行窗體可以看到系統是支持完全自定義 紅 綠藍顏色分量的的顏色的,不知道系統為什么自己不開放,好像vista里面的api有支持了,不過MSDN也不做詳細解釋。
#include "stdafx.h"
#include <windows.h>
using namespace std ;
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hConsole;
int k;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// you can loop k higher to see more color choices
for(k = 1; k < 255; k++) {
// pick the colorattribute k you want
SetConsoleTextAttribute(hConsole, k);
cout << k << " I want to be nice today!" << endl;
}
cin.get(); // wait
return 0;
}