由于某些编译器的不同,某些编译器里面没有特定的一些函数 ,所以我们用vs2013来示例(vs2013里面的conio头文件里面没有特定函数,所以我们得自己写)
一、设置文本颜色函数
再#include<math.h>里面有某些函数(下面的代码为自己的函数)
int textbackground(short iColor)
{
HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbInfo;
GetConsoleScreenBufferInfo(hd, &csbInfo);
return SetConsoleTextAttribute(hd, (iColor << 4) | (csbInfo.wAttributes&~0xF0));
}
二、源代码示例
#include <conio.h>
#include<stdio.h>
#include<Windows.h>
#include<math.h>
int textbackground(short iColor)
{
HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbInfo;
GetConsoleScreenBufferInfo(hd, &csbInfo);
return SetConsoleTextAttribute(hd, (iColor << 4) | (csbInfo.wAttributes&~0xF0));
}
int main()
{
int color;
for (color = 0; color < 8; color++)
{
textbackground(color);/*设置文本的背景颜色*/
cprintf("This is color %d\r\n", color);
cprintf("Press any key to continue\r\n");
getch();/*输入字符看不见*/
}
system("pause");
return 0;
}