[英]c++ set console text color to RGB value
I want to set the text color of the console to a RGB color. 我想将控制台的文本颜色设置为RGB颜色。 I created a function to get the ColorTable of the console and change the colors in it, but it doesn't work.
我创建了一个函数来获取控制台的ColorTable并更改其中的颜色,但是它不起作用。 I don't know how to set the text color to a value from the color table so I just change the whole color table, but it doesn't do anything.
我不知道如何将颜色设置为颜色表中的值,所以我只是更改整个颜色表,但是它什么也没做。
void setColor(int r, int g, int b)
{
COLORREF cr;
cr = RGB(r, g, b);
PCONSOLE_SCREEN_BUFFER_INFOEX ci;
CONSOLE_SCREEN_BUFFER_INFOEX cir;
ci = ○
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfoEx(hConsole , ci);
cout <<hex<< ci->ColorTable[2];
for(int i=0;i<16;i++){
ci->ColorTable[i] = cr;
}
SetConsoleScreenBufferInfoEx(hConsole, ci);
GetConsoleScreenBufferInfoEx(hConsole , ci);
cout <<endl << ci->ColorTable[2];
}
In main()
I invoke the function multiple times, but the output is the same every call and the color doesn't change. 在
main()
我多次调用该函数,但是每次调用的输出都是相同的,并且颜色不变。 SetConsoleScreenBufferInfoEx()
and GetConsoleScreenBufferInfoEx()
don't seem to do anything, ci
remains unchanged when they are called. SetConsoleScreenBufferInfoEx()
和GetConsoleScreenBufferInfoEx()
似乎什么也没做,调用ci
保持不变。
What do I do wrong? 我做错了什么?
Also, if it worked I assume the background color would also get changed because I change the whole pallette, so how do I set the text color to a specific value from the color table, eg i put ci->ColorTable[2] = cr;
另外,如果可行,我认为背景颜色也会因为更改整个调色板而得到改变,因此如何从颜色表中将文本颜色设置为特定值,例如我将
ci->ColorTable[2] = cr;
in the changeColor()
function instead of the for loop, how can I set the text color to the color that is now stored in ColorTable[2]
? 在
changeColor()
函数而不是for循环中,如何将文本颜色设置为现在存储在ColorTable[2]
的颜色?
您需要使用SetConsoleTextAttribute
设置当前的文本颜色和背景颜色,有关详细信息,请参见http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms686047(v=vs.85).aspx 。
A Windows console color table looks like this: Windows控制台颜色表如下所示:
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;
蓝色为1,白色为15;
Hence int backgroundcolor=1;
因此
int backgroundcolor=1;
and int foregroundcolor=15;
和
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.