簡體   English   中英

如何設置永久背景顏色?

[英]How to set permanent background colour?

所以我需要幫助:

//
//     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)
}

因此,這段代碼在運行時會生成我希望它生成的所有文本,但隨之而來的是背景顏色的問題。 除了正在用它編寫的文本之外,背景不會加載,這讓我不得不使用它:

system("color ___")

該空白留給文本顏色值。 但基本上這段代碼(在我眼里是討厭的)所做的不僅是設置背景顏色和前景色的值,而且它改變了文本后面的相同值,!! 這就是讓我想把這段代碼扔進坑里的原因,因為我只是想到處獲取背景色,而不是改變前景色!

請幫忙。 :(

@HansPassant 指出了解決方案。 您可以檢查以下代碼和結果是否是您要查找的內容。

代碼:

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";
}

結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM