繁体   English   中英

10,000行后控制台中的c ++错误的背景颜色

[英]c++ wrong background colors in console after 10'000 lines

在打印的行数超过“Screen Buffor Size”的高度(默认为300,最大为9999)时,第一条白线的其余部分为黄色背景。 信息连续显示,重要部分突出显示,因此system("cls")不是首选选项。 还有其他解决方案吗?

#include "Windows.h"
#include "stdio.h"

class Console {
    HANDLE h;
public:
    Console() { h = GetStdHandle(STD_OUTPUT_HANDLE); }
    void ChangeColor(WORD wColor) {
        if (h != INVALID_HANDLE_VALUE) SetConsoleTextAttribute(h, wColor);
    }
} console;

int main() {
    for (int i = 0;; i += 4) {
        console.ChangeColor(224);
        printf("highligted yellow, line = %5d\n", i);

        console.ChangeColor(240);
        printf("plane white\n\n\n");

        if (i %  1000 == 0) getchar();
    }
}

我认为控制台缓冲区被重用,你设置的颜色属性会弄乱新行。 您应该分配黑色并单独处理EOL。

for (int i = 0;; i += 4)
{
    console.ChangeColor(224);
    printf("highligted yellow, line = %d", i);
    console.ChangeColor(0);
    printf("\n");
    console.ChangeColor(240);
    printf("plane white");
    console.ChangeColor(0);
    printf("\n\n\n");
    if (0 == (i %  1000)) getchar();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM