欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

分享我写的颜色打印表代码  

程序员文章站 2022-05-19 20:27:34
...
C/C++ code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->#include <iostream> #include <windows.h> bool SetConsoleColor(WORD forceGroundColor, WORD backGroundColor); bool SetConsoleColor(WORD Color); int main() { SetConsoleColor(FOREGROUND_INTENSITY | FOREGROUND_RED , BACKGROUND_RED | BACKGROUND_GREEN | 0x80); std::cout << "明天就是2012了。你们还有心情搞技术嘛?" << std::endl; SetConsoleColor(9); //高亮蓝 1+8 默认黑背景 std::cout << "明天就是2012了。你们还有心情搞技术嘛?" << std::endl; SetConsoleColor(0x89); //高亮蓝 1+8 背景灰色 std::cout << "明天就是2012了。你们还有心情搞技术嘛?" << std::endl; SetConsoleColor(7); //高亮蓝 1+8 背景灰色 std::cout << "明天就是2012了。你们还有心情搞技术嘛?" << std::endl; //直接调用 高亮红 默认黑背景 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED); system("pause"); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0xA); //亮绿 return 0; } /* 参数1和参数2分别是前景的颜色和背景的颜色。 * #define FOREGROUND_BLUE 0x1 * #define FOREGROUND_GREEN 0x2 * #define FOREGROUND_RED 0x4 * #define FOREGROUND_INTENSITY 0x8 //前景高亮 * #define BACKGROUND_BLUE 0x10 * #define BACKGROUND_GREEN 0x20 * #define BACKGROUND_RED 0x40 * #define BACKGROUND_INTENSITY 0x80 //背景高亮 */ /*颜色参数直接使用 White_Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); Red_Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED); Green_Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_GREEN); Yellow_Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN); Blue_Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_BLUE); Magenta_Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE); Cyan_Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE); Black_Gray: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY | BACKGROUND_INTENSITY); Black_White: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); Red_White: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED); */ bool SetConsoleColor(WORD forceGroundColor, WORD backGroundColor) { // 获得缓冲区句柄。参数可以选择一下三种值: //// STD_INPUT_HANDLE 标准输入的句柄 //// STD_OUTPUT_HANDLE 标准输出的句柄 //// STD_ERROR_HANDLE 标准错误的句柄 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); if (handle == 0) return false; //设置文本及背景颜色。 BOOL ret = SetConsoleTextAttribute(handle, forceGroundColor | backGroundColor); return(ret == TRUE); } bool SetConsoleColor(WORD Color) { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); if (handle == 0) return false; BOOL ret = SetConsoleTextAttribute(handle, Color); return(ret == TRUE); }