如何在VC++2008中的win32控制台应用程序中改变字体颜色???
比方说有chara,b,c,我想在输出的时候a为蓝色,b为绿色,c为红色,怎么办啊???最近在做大作业,里面的字要不同颜色来区分,希望各位大神不吝赐教,最好写的简单易懂点...
比方说有char a,b,c,我想在输出的时候a为蓝色,b为绿色,c为红色,怎么办啊???最近在做大作业,里面的字要不同颜色来区分,希望各位大神不吝赐教,最好写的简单易懂点
展开
展开全部
如果是C/C++程序的话:
// 首先引入头文件 ConsoleColor.h 或 Windows.h
然后在想改变输出文字颜色的地方加以下语句:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x0B);
然后用正常的输出语句输出自己的内容即可。但是每改变一次颜色就要调用一下这个函数。
例子:
White on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
Red on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED);
Green on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_GREEN);
Yellow on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
Blue on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_BLUE);
Magenta on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
Cyan on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
Black on Gray: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY | BACKGROUND_INTENSITY);
Black on White: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY | FOREGROUND_INTENSITY | BACKGROUND_RED | ACKGROUND_GREEN | BACKGROUND_BLUE);
Red on White: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED);
// 首先引入头文件 ConsoleColor.h 或 Windows.h
然后在想改变输出文字颜色的地方加以下语句:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x0B);
然后用正常的输出语句输出自己的内容即可。但是每改变一次颜色就要调用一下这个函数。
例子:
White on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
Red on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED);
Green on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_GREEN);
Yellow on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
Blue on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_BLUE);
Magenta on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
Cyan on Black: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
Black on Gray: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY | BACKGROUND_INTENSITY);
Black on White: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY | FOREGROUND_INTENSITY | BACKGROUND_RED | ACKGROUND_GREEN | BACKGROUND_BLUE);
Red on White: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY | FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_RED);
参考资料: http://hi.baidu.com/ylof1986/blog/item/5a46a31e8c1c4619403417c7.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询