【问题标题】:How to change the background color of the whole console without affecting the text color in c++ on windows OS如何更改整个控制台的背景颜色而不影响windows OS上c++中的文本颜色
【发布时间】: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


    【解决方案1】:

    您需要自己实现。 Windows Console API 可以提供帮助,所有功能都是documented here

    您可以使用ReadConsoleOutputAttributes 读取当前显示字符的颜色。您可以使用WriteConsoleOutputAttributes 将它们更改为新的背景颜色。

    【讨论】:

      猜你喜欢
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 2011-05-21
      • 2011-11-23
      • 1970-01-01
      • 2017-03-25
      相关资源
      最近更新 更多