【发布时间】:2017-05-10 13:03:59
【问题描述】:
我正在尝试制作一个具有文本颜色和背景颜色属性的函数。文本颜色属性只是设置要跟随的文本颜色,而背景颜色必须改变整个控制台窗口的背景颜色。
问题在于SetConsoleTextAttribute() 函数只改变文本块的背景颜色,而不改变整个控制台窗口的背景颜色。 system("Color code") 的问题在于它也会改变任何预先编写的文本的颜色。
我想做的是这样的:
int main()
{
setColor("Red","Blue");
//custom function setColor() to set text color Red and Console's color Blue.
cout << "This is Red text on a Blue console window" << endl;
setColor("Yellow","Black"); /*now the whole console's color should turn Black, the color
of pre-written text above remains Red and the color of text
to follow should be Yellow.*/
cout << "This is Yellow text on a Black console window, The previous text is still Red";
return 0;
}
我尝试混合使用 system() 和 setConsoleTextAttribute 函数来实现这一点,但我未能在更改要跟随的文本颜色时保留预写文本的颜色。
那么,有没有一种方法可以制作一个与本例中的setColor() 函数做同样事情的函数?
【问题讨论】:
标签: c++ windows colors background-color windows-console