C语言中文字颜色怎么设置?

我用textcolor()设置字体颜色,记得我们老师说过只有用cprintf()输出的文字才会有颜色的。可是现在是cprintf()和printf()输出的文字全都有颜色... 我用textcolor()设置字体颜色,记得我们老师说过只有用cprintf()输出的文字才会有颜色的。可是现在是cprintf()和printf()输出的文字全都有颜色了。我想知道为什么?有什么办法只给特定的文字设置颜色? 展开
 我来答
L_o_o_n_i_e
推荐于2017-10-05 · TA获得超过4.2万个赞
知道大有可为答主
回答量:8507
采纳率:38%
帮助的人:5082万
展开全部
只给特定的文字设置颜色不太容易。

这里有C++ 源程序,看看你能不能改成C。
(见参考资料)

#include "console.h"
using namespace std;
void clrscr()
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '),
dwConSize,
coordScreen,
&cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole,
csbi.wAttributes,
dwConSize,
coordScreen,
&cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
}
void gotoxy(int x, int y)
{
COORD point;
point.X = x; point.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),
point);
}

void setrgb(int color)
{
switch (color)
{
case 0: // White on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE);
break;
case 1: // Red on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED);
break;
case 2: // Green on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_GREEN);
break;
case 3: // Yellow on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED |
FOREGROUND_GREEN);
break;
case 4: // Blue on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_BLUE);
break;
case 5: // Magenta on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED |
FOREGROUND_BLUE);
break;
case 6: // Cyan on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_GREEN |
FOREGROUND_BLUE);
break;
case 7: // Black on Gray
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
BACKGROUND_INTENSITY | BACKGROUND_INTENSITY);
break;
case 8: // Black on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE);
break;
case 9: // Red on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE |
FOREGROUND_RED);
break;
case 10: // Green on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE | FOREGROUND_GREEN);
break;
case 11: // Yellow on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE | FOREGROUND_RED |
FOREGROUND_GREEN);
break;
case 12: // Blue on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE | FOREGROUND_BLUE);
break;
case 13: // Magenta on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE | FOREGROUND_RED |
FOREGROUND_BLUE);
break;
case 14: // Cyan on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE | FOREGROUND_GREEN |
FOREGROUND_BLUE);
break;
case 15: // White on White
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
BACKGROUND_INTENSITY | FOREGROUND_INTENSITY |
BACKGROUND_RED | BACKGROUND_GREEN |
BACKGROUND_BLUE | FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE);
break;
default : // White on Black
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE);
break;
}
}

参考资料: http://www.codeguru.com/Cpp/W-D/console/article.php/c3959/

求是的梦
2008-05-21 · TA获得超过293个赞
知道小有建树答主
回答量:526
采纳率:0%
帮助的人:218万
展开全部
没有标准库作这些事情
和你用的开发环境有关
tc的话是 conio.h
textcolor函数
vc参考ls
在linux下直接把颜色情况加在printf中,参考linux终端颜色编码
printf("\033[31mThe color of this line is red!\033[0m");
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
头子赴3
推荐于2017-10-06 · TA获得超过3102个赞
知道小有建树答主
回答量:1592
采纳率:25%
帮助的人:469万
展开全部
可以用window.h 里面的setcolor函数,参考下面的例子,
#include<iostream>
#include<windows.h>
using namespace std;

void SetColor(unsigned short ForeColor,unsigned short BackGroundColor)
{HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,(ForeColor%16)|(BackGroundColor%16*16));}

int main()
{
int i,j;
while(cin>>i>>j)
{
SetColor(i,j);
cout<<"加油,中国!!"<<endl;
SetColor(7,0);///恢复为黑底白字。。
}
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式