C++ 如何更改文本和背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9965710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change text and background color?
提问by Tprice88
I want every character to be a different color.
我希望每个角色都是不同的颜色。
for example,
例如,
cout << "Hello world" << endl;
- Hwould be red
- ewould be blue
- lwould be orange and so on.
- H会变红
- e会是蓝色的
- 我会是橙色的等等。
I know this can be done, I just don't know the code for it.
我知道这可以做到,我只是不知道它的代码。
and I want to change the background color to white. How would I do that?
我想将背景颜色更改为白色。我该怎么做?
回答by ApprenticeHacker
There is no (standard) cross-platform way to do this. On windows, try using conio.h
.
It has the:
没有(标准的)跨平台方法可以做到这一点。在 Windows 上,尝试使用conio.h
. 它具有:
textcolor(); // and
textbackground();
functions.
职能。
For example:
例如:
textcolor(RED);
cprintf("H");
textcolor(BLUE);
cprintf("e");
// and so on.
回答by Mike Kwan
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdOut, FOREGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
This would produce red text on a white background.
这将在白色背景上产生红色文本。
回答by Mike Kwan
You can use the function system
.
您可以使用该功能system
。
system("color *background**foreground*");
For background and foreground, type in a number from 0 - 9 or a letter from A - F.
对于背景和前景,请输入 0 - 9 之间的数字或 A - F 中的字母。
For example:
例如:
system("color A1");
std::cout<<"hi"<<std::endl;
That would display the letters "hi" with a green background and blue text.
这将显示带有绿色背景和蓝色文本的字母“hi”。
To see all the color choices, just type in:
要查看所有颜色选择,只需输入:
system("color %");
to see what number or letter represents what color.
看看什么数字或字母代表什么颜色。
回答by Kacper Banasik
You can also use PDCurses library. (http://pdcurses.sourceforge.net/)
您还可以使用 PDCurses 库。(http://pdcurses.sourceforge.net/)
回答by Mujahid Riaz
`enter code here`#include <stdafx.h> // Used with MS Visual Studio Express. Delete line if using something different
#include <conio.h> // Just for WaitKey() routine
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); // For use of SetConsoleTextAttribute()
void WaitKey();
int main()
{
int len = 0,x, y=240; // 240 = white background, black foreground
string text = "Hello World. I feel pretty today!";
len = text.length();
cout << endl << endl << endl << "\t\t"; // start 3 down, 2 tabs, right
for ( x=0;x<len;x++)
{
SetConsoleTextAttribute(console, y); // set color for the next print
cout << text[x];
y++; // add 1 to y, for a new color
if ( y >254) // There are 255 colors. 255 being white on white. Nothing to see. Bypass it
y=240; // if y > 254, start colors back at white background, black chars
Sleep(250); // Pause between letters
}
SetConsoleTextAttribute(console, 15); // set color to black background, white chars
WaitKey(); // Program over, wait for a keypress to close program
}
void WaitKey()
{
cout << endl << endl << endl << "\t\t\tPress any key";
while (_kbhit()) _getch(); // Empty the input buffer
_getch(); // Wait for a key
while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}
回答by P.D.K.R.Gunatilaka
Colors are bit-encoded. If You want to change the Text color in C++ language There are many ways. In the console, you can change the properties of output.click this icon of the console and go to properties and change color.
颜色是位编码的。如果你想在 C++ 语言中改变 Text 颜色有很多方法。在控制台中,您可以更改输出的属性。单击控制台的此图标并转到属性并更改颜色。
The second way is calling the system colors.
第二种方法是调用系统颜色。
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
//Changing Font Colors of the System
system("Color 7C");
cout << "\t\t\t ****CEB Electricity Bill Calculator****\t\t\t " << endl;
cout << "\t\t\t *** MENU ***\t\t\t " <<endl;
return 0;
}