平时在VC6.0中编写Console程序时,系统默认字符的背景色为黑色,前景色为白色。 现在我们来实现Console输出彩色字符. include<afx.h> //其内包含了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)