目录
题目
问题分析
代码
运行结果
题目
Press any key to change color(按任意键改变颜色)
问题分析
在VS 2019编译器中没有textbackground这个库函数,所以需要手动写一个具有同样功能的函数,我们把这个函数的名称也定义为textbackground。输入字符看不见的函数在VS 2019中是_getch()函数。
代码
vs2019
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>
#include<math.h>
int textbackground(int gColor)
{
HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbInfo;
GetConsoleScreenBufferInfo(hd, &csbInfo);
return SetConsoleTextAttribute(hd, (gColor << 4) | (csbInfo.wAttributes & ~0xF0));
}
int main()
{
int color;
for (color = 0; color < 17; color++)
{
textbackground(color);//改变文本的背景颜色
printf("This is color %d\r\n", color);
printf("Press any key to continue\r\n");
_getch(); //输入字符看不见
}
system("pause");
return 0;
}
运行结果