c++ - 如何设置永久背景颜色?
问题描述
所以我需要帮助:
//
// 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 ___")
该空白留给文本颜色值。但本质上,这段代码(在我眼里是讨厌的)所做的不仅是设置背景颜色和前景色的值,而且它改变了文本 BEHIND IT 的相同值!!!这就是让我想把这段代码扔进坑里的原因,因为我只是想到处获取背景色,而不是改变前景色!
请帮忙。:(
解决方案
@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";
}
结果:
推荐阅读
- vue.js - Replace sort icon in b-table with custom SVG
- oauth - 为什么 OAuth 请求中没有 CSRF 令牌
- angular - 我希望“其他”案例只有在他们取消选中所有字段后才能工作,我该如何处理?
- hbase - 将凤凰表中的列数据类型从 VARCHAR 更改为 BIGINT
- html - 如何在不发生这种情况的情况下在 CSS 中定位按钮?
- r - ggplot2:将多行图例标题与数学表达式对齐
- android - Recycerview 网格布局的动态高度
- javascript - Deleting a todo - MERN Stack
- javascript - MaterialUI V5 中如何导入组件?
- python - Opencv:移除背景并在身份证周围绘制边界框