Console终端输出彩色字符和彩色背景

平时在VC6.0中编写Console程序时,系统默认字符的背景色为黑色,前景色为白色。

现在我们来实现Console输出彩色字符.

 

include //其内包含了windows.h #include"stdio.h" void ConSetTextColor(unsigned char frontColor, unsigned char backColor) { /*GetStdHandle函数可以获得标准输入、输出、错误设备屏 幕缓冲区的句柄,*/ HANDLE hWnd=::GetStdHandle(STD_OUTPUT_HANDLE);//标准输出设备,同理:STD_INPUT_HANDLE, STD_ERROR_HANDLE. backColor<<=4; //有16中颜色可选择,高4为背景色,低4为前景色。 backColor+=frontColor; ::SetConsoleTextAttribute(hWnd, backColor); //设置背景色与前景色的函数 } int main() { /*======================================== int input; CString str; printf("Please entry a positive number:"); scanf("%d", &input); str.Format(TEXT("Your Number is: %d"), input); MessageBox(NULL, str, TEXT("Input"), MB_OK); ==========================================*/ for (unsigned char front=0; front<16; front+=1) { for (unsigned char back=0; back<16; back+=1) { ConSetTextColor(0, back); //设置背景色 printf(TEXT("front=%d, back=%d"), front, back); ConSetTextColor(0,0); //都为黑色 printf(TEXT(" ")); ConSetTextColor(front, 0); //设置前景色 printf(TEXT("front=%d, back=%d"), front, back); ConSetTextColor(0,0); printf(TEXT(" ")); ConSetTextColor(front, back); //都设置 printf(TEXT("front=%d, back=%d/n"), front, back); for (int i=0; i<10000000; i++) ; //延时 } } return 0; }

 

 

效果如下:

你可能感兴趣的:(Windows,程序设计)