【发布时间】:2018-12-12 01:40:38
【问题描述】:
由于 BGI 已经过时,而且它的很多源代码似乎从原始网站中丢失,我一直打算设计我自己的颜色引擎来单独影响线条。到目前为止,windows.h 中的“SetConsoleTextAttribute ()”可以接受的 16 种颜色都做得很好,但我一直打算使用更多颜色(通过使用 RGB 而不是 0xbf)来升级它的外观并为我的颜色着色自己的ASCII艺术。
“SetTextColor()”似乎是我想走的路线。我已经设置了一个测试功能,看看它是否有效。这是带有设置的代码的 sn-p。
HDC hType; // Handle DC, save some work to reduce repetition
int initColor () // Initializes engine
{
hType = GetDC (GetConsoleWindow ());
printf ("String Hexadecimal\n");
testcolorR (RGB(255, 0, 0)); // Red
testcolorR (RGB(0, 255, 0)); // Green
testcolorR (RGB(0, 0, 255)); // Blue
getch (); // Pause to see results
return 0; // Exit success
}
// Take in RGB
void colortextR (COLORREF rgbcolor)
{
SetTextColor (hType, rgbcolor);
}
// Test RGB colors
int testcolorR (COLORREF color)
{
colortextR (color);
printf ("Hello %#x\n", color);
return 0;
}
但是,在命令行中,颜色并没有改变,仍然是默认的浅灰色,但这就是结果。
- 十六进制字符串
- 你好 0xff
- 你好 0xff00
- 你好 0xff0000
这意味着正在传递 RGB 颜色,但其他原因导致了此问题。我怀疑罪魁祸首是 GetConsoleWindow () 函数。
【问题讨论】:
-
或者控制台窗口可能不像你想象的那样工作
-
SetTextColor()不是处理这个问题的方法。您必须使用SetConsoleTextAttribute(),因此您最多只能使用 16 种颜色 - 但是 - 您可以使用SetConsoleScreenBufferInfoEx()将这 16 种颜色设置为您想要的任何 RGB 值。请参阅this answer 至 RGB Specific Console Text Color -
所以不幸的是,除了修改 16 种颜色之外,我没有其他解决方法。如果有其他方法,我仍然愿意接受任何其他可能有效的解决方案。
-
编写自己的控制台(或使用第三方的)?
-
我想我之前尝试过这样做,但实际上最终只使用了 16 种颜色。
cmd似乎不支持您想要的