原文出处:
https://blog.csdn.net/odaynot/article/details/7722240
混合颜色
#include <windows.h>
#include <iostream>
using namespace std;int main()
{HANDLE hOut;hOut = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(hOut,FOREGROUND_RED | FOREGROUND_GREEN);cout << "This text is yellow." << endl;SetConsoleTextAttribute(hOut,FOREGROUND_GREEN | FOREGROUND_BLUE);cout << "This text is cyan." << endl;SetConsoleTextAttribute(hOut,FOREGROUND_BLUE | FOREGROUND_RED);cout << "This text is magenta." << endl;SetConsoleTextAttribute(hOut,FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);cout << "This text is white." << endl;return 0;
}
明暗
#include <windows.h>
#include <iostream>
using namespace std;int main()
{HANDLE hOut;hOut = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(hOut,FOREGROUND_RED);cout << "Red " << flush;SetConsoleTextAttribute(hOut,FOREGROUND_RED |FOREGROUND_INTENSITY);cout << "Red" << endl;SetConsoleTextAttribute(hOut,FOREGROUND_GREEN);cout << "Green " << flush;SetConsoleTextAttribute(hOut,FOREGROUND_GREEN |FOREGROUND_INTENSITY);cout << "Green" << endl;SetConsoleTextAttribute(hOut,FOREGROUND_BLUE);cout << "Blue " << flush;SetConsoleTextAttribute(hOut,FOREGROUND_BLUE |FOREGROUND_INTENSITY);cout << "Blue" << endl;SetConsoleTextAttribute(hOut,FOREGROUND_RED | FOREGROUND_GREEN);cout << "Yellow " << flush;SetConsoleTextAttribute(hOut,FOREGROUND_RED | FOREGROUND_GREEN |FOREGROUND_INTENSITY);cout << "Yellow" << endl;SetConsoleTextAttribute(hOut,FOREGROUND_GREEN | FOREGROUND_BLUE);cout << "Cyan " << flush;SetConsoleTextAttribute(hOut,FOREGROUND_GREEN | FOREGROUND_BLUE |FOREGROUND_INTENSITY);cout << "Cyan" << endl;SetConsoleTextAttribute(hOut,FOREGROUND_BLUE | FOREGROUND_RED);cout << "Magenta " << flush;SetConsoleTextAttribute(hOut,FOREGROUND_BLUE | FOREGROUND_RED |FOREGROUND_INTENSITY);cout << "Magenta" << endl;SetConsoleTextAttribute(hOut,FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);cout << "White " << flush;SetConsoleTextAttribute(hOut,FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE |FOREGROUND_INTENSITY);cout << "White" << endl;return 0;
}
背景色
#include <windows.h>
#include <iostream>
using namespace std;int main()
{HANDLE hOut;hOut = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleTextAttribute(hOut,BACKGROUND_RED);cout << "Red " << flush;SetConsoleTextAttribute(hOut,BACKGROUND_RED |BACKGROUND_INTENSITY);cout << "Red " << endl;SetConsoleTextAttribute(hOut,BACKGROUND_GREEN);cout << "Green " << flush;SetConsoleTextAttribute(hOut,BACKGROUND_GREEN |BACKGROUND_INTENSITY);cout << "Green " << endl;SetConsoleTextAttribute(hOut,BACKGROUND_BLUE);cout << "Blue " << flush;SetConsoleTextAttribute(hOut,BACKGROUND_BLUE |BACKGROUND_INTENSITY);cout << "Blue " << endl;SetConsoleTextAttribute(hOut,BACKGROUND_RED |BACKGROUND_GREEN);cout << "Yellow " << flush;SetConsoleTextAttribute(hOut,BACKGROUND_RED |BACKGROUND_GREEN |BACKGROUND_INTENSITY);cout << "Yellow " << endl;SetConsoleTextAttribute(hOut,BACKGROUND_GREEN |BACKGROUND_BLUE);cout << "Cyan " << flush;SetConsoleTextAttribute(hOut,BACKGROUND_GREEN |BACKGROUND_BLUE |BACKGROUND_INTENSITY);cout << "Cyan " << endl;SetConsoleTextAttribute(hOut,BACKGROUND_BLUE |BACKGROUND_RED);cout << "Magenta " << flush;SetConsoleTextAttribute(hOut,BACKGROUND_BLUE |BACKGROUND_RED |BACKGROUND_INTENSITY);cout << "Magenta " << endl;SetConsoleTextAttribute(hOut,BACKGROUND_RED |BACKGROUND_GREEN |BACKGROUND_BLUE);cout << "White " << flush;SetConsoleTextAttribute(hOut,BACKGROUND_RED |BACKGROUND_GREEN |BACKGROUND_BLUE |BACKGROUND_INTENSITY);cout << "White " << endl;return 0;
}