C语言经典100例题(32)--Press any key to change color(按任意键改变颜色)

2023-11-16

目录

题目

问题分析

代码

运行结果


题目

 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;
}

 

运行结果

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C语言经典100例题(32)--Press any key to change color(按任意键改变颜色) 的相关文章

随机推荐