简体   繁体   English

如何设置永久背景颜色?

[英]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. @HansPassant 指出了解决方案。 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:结果:

在此处输入图像描述

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

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