如何更改文本和背景颜色?

时间:2022-11-20 22:56:26

I want every character to be a different color.

我希望每个角色都是不同的颜色。

for example,

例如,

cout << "Hello world" << endl;
  • H would be red
  • H是红色的
  • e would be blue
  • e是蓝色的
  • l would be orange and so on.
  • l是橙色的,等等。

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?

我想把背景颜色改成白色。我该怎么做呢?

6 个解决方案

#1


8  

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.

#2


4  

SetConsoleTextAttribute.

SetConsoleTextAttribute。

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.

这将产生白色背景上的红色文本。

#3


4  

You can use the function 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.

看看哪个数字或字母代表什么颜色。

#4


2  

You can also use PDCurses library. (http://pdcurses.sourceforge.net/)

您还可以使用PDCurses库。(http://pdcurses.sourceforge.net/)

#5


1  

`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)
}

#6


0  

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.

颜色是bit-encoded。如果您想在c++语言中更改文本颜色,有很多方法。在控制台中,您可以更改输出的属性。单击控制台的图标,进入属性并更改颜色。

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;

}

#1


8  

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.

#2


4  

SetConsoleTextAttribute.

SetConsoleTextAttribute。

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.

这将产生白色背景上的红色文本。

#3


4  

You can use the function 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.

看看哪个数字或字母代表什么颜色。

#4


2  

You can also use PDCurses library. (http://pdcurses.sourceforge.net/)

您还可以使用PDCurses库。(http://pdcurses.sourceforge.net/)

#5


1  

`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)
}

#6


0  

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.

颜色是bit-encoded。如果您想在c++语言中更改文本颜色,有很多方法。在控制台中,您可以更改输出的属性。单击控制台的图标,进入属性并更改颜色。

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;

}