0
system("color F0");

Меняю цвет консоли этой строчкой, вопрос вот в чем. Как после конкретных манипуляций восстановить дефолтный цвет консоли?

1 ответ 1

1

system("color F0"); - считается не корректным использованием system();

Please don't tell people to use system(). If you are going to do something OS-dependant, do it the right way.

Первоисточник
И приведенный код right way

#include <iostream>
#include <windows.h>

int main()
{
    const WORD colors[] =
        {
        0x1A, 0x2B, 0x3C, 0x4D, 0x5E, 0x6F,
        0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6
        };

    HANDLE hstdin  = GetStdHandle( STD_INPUT_HANDLE  );
    HANDLE hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
    WORD   index   = 0;

    // Remember how things were when we started
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo( hstdout, &csbi );

    // Tell the user how to stop
    SetConsoleTextAttribute( hstdout, 0xEC );
    std::cout << "Press any key to quit.\n";

    // Draw pretty colors until the user presses any key
    while (WaitForSingleObject( hstdin, 100 ) == WAIT_TIMEOUT)
    {
        SetConsoleTextAttribute( hstdout, colors[ index ] );
        std::cout << "\t\t\t\t Hello World \t\t\t\t" << std::endl;
        if (++index > sizeof(colors)/sizeof(colors[0]))
            index = 0;
    }
    FlushConsoleInputBuffer( hstdin );

    // Keep users happy
    SetConsoleTextAttribute( hstdout, csbi.wAttributes );
    return 0;
}

Похожий вопрос на stackoverflow

Если очень нужно... установить default значениe, то setColor(8 , 15);
Надеюсь помог!

2
  • Встречный вопрос. Что делать если изменить нужно фон всей консоли а не только под текстом?
    – user239757
    7 мая 2017 в 0:21
  • setColor(0, 15); - 0 - черный фон, 15 - белые буквы.
    – Brave_Lime
    7 мая 2017 в 8:25

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.