Hi. I try to change the text color of console in C++. I know some commands like .
system("color fg");
But I want to change the color of only one line of console. Is this possible? Would someone help me, please?
Hi. I try to change the text color of console in C++. I know some commands like .
system("color fg");
But I want to change the color of only one line of console. Is this possible? Would someone help me, please?
On Windows, you can color just one line by temporarily changing the console text attributes before printing that line, then restoring the original attributes. Avoid system("color ...") for this since it affects the whole console session. Using the Win32 API also lets you use named bit flags (e.g., FOREGROUND_RED) instead of hard-coded numbers.
#include <windows.h>
#include <iostream>
void print_colored_line(const std::string& s, WORD attrs)
{
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi{};
if (!GetConsoleScreenBufferInfo(h, &csbi)) {
std::cout << s << "\n"; // Fallback if not a console
return;
}
WORD old = csbi.wAttributes;
SetConsoleTextAttribute(h, attrs);
std::cout << s << "\n";
SetConsoleTextAttribute(h, old); // restore
}
int main()
{
// Example: bright cyan text on default background
WORD attrs = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
print_colored_line("Only this line is colored", attrs);
std::cout << "This line uses the original color\n";
} For finer control (e.g., coloring specific characters or an entire buffer line at a given coordinate), look at WriteConsoleOutputAttribute or FillConsoleOutputAttribute. On Windows 10+, you can also enable virtual terminal processing and print ANSI escape sequences for colors; see SetConsoleMode with ENABLE_VIRTUAL_TERMINAL_PROCESSING and Microsoft’s Console Virtual Terminal Sequences. API references: SetConsoleTextAttribute, GetConsoleScreenBufferInfo.
on Windows OS.
Thanks, nullptr!
A Windows console color table looks like this:
Color Background Foreground
---------------------------------------------
Black 0 0
Blue 1 1
Green 2 2
Cyan 3 3
Red 4 4
Magenta 5 5
Brown 6 6
White 7 7
Gray - 8
Intense Blue - 9
Intense Green - 10
Intense Cyan - 11
Intense Red - 12
Intense Magenta - 13
Yellow - 14
Intense White - 15
To set background colors you have to combine the foreground color code with
the background color code using this equation:
finalcolor = (16*backgroundcolor) + foregroundcolor
if you want to set a text color that has a blue background and white text
you simply look up the color code in the table. Blue is 1 and white is 15;
Hence int backgroundcolor=1; and int foregroundcolor=15;
#include <windows.h>
#include <iostream>
using namespace std;
void setcolor(int color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
return;
}
int main()
{
int foregroundcolor=15;
int backgroundcolor=1;
int finalcolor;
finalcolor=(16*backgroundcolor)+foregroundcolor;
setcolor(finalcolor);
cout<<"finalcolor=(16*backgroundcolor)+foregroundcolor\n";
setcolor(7);
return 0;
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.