簡體   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