简体   繁体   中英

How to set permanent background colour?

So I need help:

//
//     PracticeModule - Practice file
//
#include <iostream>
#include <conio.h>
#include <cstdio>
#include <cstdlib>
#include <windows.h>
using namespace std;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); // For use of SetConsoleTextAttribute()
void WaitKey();
int main()
{
    SetConsoleTextAttribute(console,137);
    cout << "GLaDOS : Hello, and welcome to the the APERTURE SCIENCE HANDHELD PORTAL GUN TESTING INITIATIVE offices." << endl << "\n";
    SetConsoleTextAttribute(console,137);
    cout << "GLaDOS : To begin the testing cycle, please enter your standard issue APERTURE SCIENCE ALL PURPOSE EMPLOYEE  \nSECURITY KEY." << endl << "\n";
    cout << "Narrator : Uh oh! How could this have happened?! You left your security key at home by accident! You do remember reading it over quite a lot. It isnt anything to blame you of, of course. Anyone in your position would be equally nervous, if not more! It's Aperture Science! The main leading science company! To work for them is an honour! Although, your career could end today because of this. There seems to be no-one around, maybe you could look around and use one of the other's? They probably wouldn't mind. After all, you ARE doing it for the sake of your career. Besides, it would probably benefit them more than you to put in a few extra hours at work in their name." << endl<< "\n" << "So, do you look around for a security key?[1] or do you try to remember the security keycode?[2]" << endl << "\n";
    WaitKey();
}
void WaitKey()
{
    cout << "\t\t\t\t\t\tPress any key to continue...";
    while (_kbhit()) _getch(); // Empty the input buffer
    _getch(); // Wait for a key
    while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}

So this code, when run, generates all the text i want it to generate, but then there comes the problem of the background color. the background won't load except for the text that is being written with it, which makes me have to use this:

system("color ___")

That blank is left for the text colour value. but essentially what this piece of code (which is hated in my eyes) does is not only does it set the value for the background colour and foreground colour, but it changes the same values for the text BEHIND IT,!! And this is what makes me want to put this piece of code to rot in a pit because I am just trying to get the background colour everywhere, not change the foreground colour too!

Please help. :(

@HansPassant has pointed out the solution. You can check if the following code and result are what you are looking for.

Code:

void ClearScreen()
{
    HANDLE                     hStdOut;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD                      count;
    DWORD                      cellCount;
    COORD                      homeCoords = { 0, 0 };

    hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hStdOut == INVALID_HANDLE_VALUE) return;

    /* Get the number of cells in the current buffer */
    if (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return;
    cellCount = csbi.dwSize.X *csbi.dwSize.Y;

    /* Fill the entire buffer with spaces */
    if (!FillConsoleOutputCharacter(
        hStdOut,
        (TCHAR) ' ',
        cellCount,
        homeCoords,
        &count
    )) return;

    /* Fill the entire buffer with the current colors and attributes */
    if (!FillConsoleOutputAttribute(
        hStdOut,
        csbi.wAttributes,
        cellCount,
        homeCoords,
        &count
    )) return;

    /* Move the cursor home */
    SetConsoleCursorPosition(hStdOut, homeCoords);
}

int main()
{
    SetConsoleTextAttribute(console, 137);
    ClearScreen();
    cout << "GLaDOS : Hello, and welcome to the the APERTURE SCIENCE HANDHELD PORTAL GUN TESTING INITIATIVE offices." << endl << "\n";
    cout << "GLaDOS : To begin the testing cycle, please enter your standard issue APERTURE SCIENCE ALL PURPOSE EMPLOYEE  \nSECURITY KEY." << endl << "\n";
    cout << "Narrator : Uh oh! How could this have happened?! You left your security key at home by accident! You do remember reading it over quite a lot. It isnt anything to blame you of, of course. Anyone in your position would be equally nervous, if not more! It's Aperture Science! The main leading science company! To work for them is an honour! Although, your career could end today because of this. There seems to be no-one around, maybe you could look around and use one of the other's? They probably wouldn't mind. After all, you ARE doing it for the sake of your career. Besides, it would probably benefit them more than you to put in a few extra hours at work in their name." << endl << "\n" << "So, do you look around for a security key?[1] or do you try to remember the security keycode?[2]" << endl << "\n";
}

Result:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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