簡體   English   中英

如何在Windows OS上的c ++中更改整個控制台的背景顏色而又不影響文本顏色

[英]How to change the background color of the whole console without affecting the text color in c++ on windows OS

我正在嘗試使函數具有文本顏色和背景顏色屬性。 文本顏色屬性僅設置要跟隨的文本顏色,而背景顏色必須更改整個控制台窗口的背景顏色。

問題在於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()函數相同?

您將需要自己實現。 Windows控制台API可以提供幫助,所有功能均在此處記錄

您可以使用ReadConsoleOutputAttributes讀取當前顯示的字符的顏色。 您可以使用WriteConsoleOutputAttributes將它們更改為新的背景色。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM