[英]How can i change colors in c++?
So i searched for it and i found many answers. 所以我搜索了它,发现了很多答案。 I found that in c++ there's no standard cross-platform way to do that and that the operative system manages colors.
我发现在c ++中没有标准的跨平台方法可以做到这一点,并且操作系统可以管理颜色。 For example i found that on windows you can use the system("color 1") statement to change color to the text ( or foreground) and the system("color A") to change color to the background, or both system("color 1A") to change both.
例如,我发现在Windows上,您可以使用system(“ color 1”)语句将颜色更改为文本(或前景),使用system(“ color A”)更改颜色为背景,或者使用system(“颜色1A“)同时更改两者。 But this will change the whole colors, and i was wondering if there was a way to change it like even for a single character.
但这会改变整个颜色,我想知道是否有办法像单个字符一样更改它。 Like take the program that i just did as an example:
就像我刚才做的程序一样:
#include<iostream>
using namespace std; /* I prefer to use this because i think that's a huge time saver and it's also easier*/
void printRoad(int i) /* That's my function, so by this function it prints a number of times choosed by the user 4 pieces of road*/
{
int counter=1;
while (counter <= i)
{
system("color 2"); /*Here is what i was talking about. I used the system("color 2") statement to change the text color
from the default to green, but it changes the whole text.*/
cout << "** | **" << endl;
cout << "** | **" << endl;
cout << "** | **" << endl;
cout << "** | **" << endl;
counter++;
}
};
void main() /*I don't need any specific return value from either the main() and the function so i thought it was a good idea to
just use void.*/
{
cout << "How many piece of roads do you want to build?" << endl; /*Here it asks to the user what to do.*/
int pieces = 0;
cin >> pieces;
printRoad(pieces); //Here is the function call.
system("pause"); /* Because i'm using windows and i'm using Visual Studio Express 2013 I used system("pause") to pause
the program and let the user see the output.*/
}
So what if, for example, i'd like to change each piece of road color? 那么,例如,如果我想更改每条道路颜色怎么办? Like the first cout<<"** | **"<
像第一个cout <<“ ** | **” <
I also read many people complaining about the use of system("") statements. 我还读过很多人抱怨使用system(“”)语句。 I understand it because by doing so your program lose the cross-platform ability.
我理解它是因为这样做会使您的程序失去跨平台功能。 But if the thing is dependent on the system we're on, how should we do it by keeping the cross-platform ability?
但是,如果事情取决于我们所使用的系统,那么如何通过保持跨平台能力来做到这一点呢? Thanks for any answer.
感谢您的回答。
Actually you can use this instead of calling system() : 实际上,您可以使用此方法代替调用system() :
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ValueOfColour);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),ValueOfColour);
As far as I understood your problem, you only want a certain character to be in your choosen colour. 据我了解您的问题,您只希望某个字符采用您选择的颜色。 Then you need to change it back to the default value white/grey after this character was printed.
然后,在打印此字符后,需要将其更改回默认值白色/灰色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.