SetTextColor不更改命令行文本颜色

时间:2018-12-12 01:40:39

标签: c++ windows winapi

由于BGI已过时,并且原始网站上似乎缺少许多源代码,因此我一直打算设计自己的颜色引擎,该引擎将单独影响线条。到目前为止,windows.h的“ SetConsoleTextAttribute()”可以接受的16种颜色一直很好,但是我一直想使用更多颜色(通过使用RGB而不是0xbf)来升级它的外观并给我上色自己的ASCII艺术。

“ SetTextColor()”似乎是我要走的路线。我已经设置了一个测试功能,看看它是否有效。这是安装程序的代码段。

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;
}

但是,在命令行上,颜色没有改变,而是保留为默认的浅灰色,但这就是结果。

  
      
  1. 十六进制字符串
  2.   
  3. 你好0xff
  4.   
  5. 你好0xff00
  6.   
  7. 你好0xff0000
  8.   

这表示正在传递RGB颜色,但其他原因导致了此问题。我怀疑罪魁祸首是GetConsoleWindow()函数。

1 个答案:

答案 0 :(得分:1)

SetTextColor是GUI功能;它不会在标准Windows控制台中达到您想要的效果。


如果您的应用程序只能在Windows 10 build 14392或更高版本上运行,或者在(大多数)非Windows平台(例如Linux)上运行,则通常可以使用virtual terminal sequences。请注意,即使在受支持的Windows版本上,也必须显式启用VT功能:

series.dataFields.valueY = field;
series.dataFields.categoryX = "year";

如果您的应用程序需要在Windows的较早版本上运行,并且标准的16色调色板就足够了,那么类似于以下内容的内容将起作用(请参见SetConsoleTextAttributeconsole screen buffer text attributes):

/**
 * ---------------------------------------
 * This demo was created using amCharts 4.
 * 
 * For more information visit:
 * https://www.amcharts.com/
 * 
 * Documentation is available at:
 * https://www.amcharts.com/docs/v4/
 * ---------------------------------------
 */

// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end

 // Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);

// Add data
chart.data = [{
  "year": 2005,
  "income": 23.5,
  "expenses": 18.1,
  "cumi" : 13
},{
  "year": 2006,
  "income": 26.2,
  "expenses": 22.8,
  "cumi" : 13
},{
  "year": 2007,
  "income": 30.1,
  "expenses": 23.9,
  "cumi" : 13
},{
  "year": 2008,
  "income": 29.5,
  "expenses": 25.1,
  "cumi" : 13
},{
  "year": 2009,
  "income": 24.6,
  "expenses": 25,
  "cumi" : 13
}];

// Create axes
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "year";
categoryAxis.numberFormatter.numberFormat = "#";
categoryAxis.renderer.inversed = true;
categoryAxis.renderer.grid.template.location = 0;
categoryAxis.renderer.cellStartLocation = 0.1;
categoryAxis.renderer.cellEndLocation = 0.9;

var  valueAxis = chart.yAxes.push(new am4charts.ValueAxis()); 
valueAxis.renderer.opposite = true;

// Create series
function createSeries(field, name) {
  var series = chart.series.push(new am4charts.ColumnSeries());
  series.dataFields.valueY = field;
  series.dataFields.categoryX = "year";
  series.name = name;
  series.columns.template.tooltipText = "{name}: [bold]{valueX}[/]";
  series.columns.template.height = am4core.percent(100);
  series.sequencedInterpolation = true;

  var valueLabel = series.bullets.push(new am4charts.LabelBullet());
  valueLabel.label.text = "{valueX}";
  valueLabel.label.verticalCenter = "bottom";
  valueLabel.label.dx = 10;
  valueLabel.label.hideOversized = false;
  valueLabel.label.truncate = false;

  var categoryLabel = series.bullets.push(new am4charts.LabelBullet());
  categoryLabel.label.text = "{name}";
  categoryLabel.label.verticalCenter = "top";
  categoryLabel.label.dx = -10;
  categoryLabel.label.hideOversized = false;
  categoryLabel.label.truncate = false;
  
  categoryLabel.label.rotation = -90;
}

createSeries("income", "Income");
createSeries("expenses", "Expenses");
createSeries("cumi", "Cumi");