如何使用 SetConsoleTextAttribute C++
How to use SetConsoleTextAttribute C++
我搜索了无数论坛和网站,但似乎找不到答案。我正在尝试使用 SetConsoleTextAttribute 但它只影响文本。我怎样才能像命令 color 1f
那样影响整个屏幕?我的代码是:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <wincon.h>
using namespace std;
int main()
{
SetConsoleTitle("C++ CALCULATOR"); // Title of window
int x; // Decision
int a; // First Number
int b; // Second Number
int c; // Answer
HANDLE Con;
Con = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(Con, BACKGROUND_BLUE | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
cout << "CALCULATOR" << endl << endl;
cout << "1:ADDITION" << endl << "2:SUBTRACTION" << endl << "3:MULTIPLICATION";
cout << endl << "4:DIVISION" << endl << "5:EXIT" << endl;
cin >> x;
switch (x)
{
case 1: // Addition code
cout << endl << "ADDITION" << endl << "FIRST NUMBER:";
cin >> a;
cout << endl << "SECOND NUMBER:";
cin >> b;
c = a + b;
cout << endl << "ANSWER:" << c;
break;
case 2: // Subtraction code
cout << endl << "SUBTRACTION" << endl << "FIRST NUMBER:";
cin >> a;
cout << endl << "SECOND NUMBER:";
cin >> b;
c = a - b;
cout << endl << "ANSWER:" << c;
break;
case 3: // Multiplication code
cout << endl << "MULTIPLICATION" << endl << "FIRST NUMBER:";
cin >> a;
cout << endl << "SECOND NUMBER:";
cin >> b;
c = a * b;
cout << endl << "ANSWER:" << c;
break;
case 4: // Division code
cout << endl << "DIVISION" << endl << "FIRST NUMBER:";
cin >> a;
cout << endl << "SECOND NUMBER:";
cin >> b;
c = a / b;
cout << endl << "ANSWER:" << c;
break;
case 5: // Exit code
return 0;
}
}
SetConsoleTextAttribute
更改您写入控制台的新字符的属性,但不会影响控制台的现有内容。
如果您想更改已显示在控制台上的现有角色的属性,请改用 WriteConsoleOutputAttribute
。
此解决方案依赖于这些 WinAPI 函数和结构:
- GetConsoleScreenBufferInfo 获取屏幕尺寸
- FillConsoleOutputAttribute 用属性填充屏幕
- CONSOLE_SCREEN_BUFFER_INFO 存储屏幕信息的结构
代码如下:
HANDLE hCon;
CONSOLE_SCREEN_BUFFER_INFO csbiScreenInfo;
COORD coordStart = { 0, 0 }; // Screen coordinate for upper left
DWORD dwNumWritten = 0; // Holds # of cells written to
// by FillConsoleOutputAttribute
DWORD dwScrSize;
WORD wAttributes = BACKGROUND_BLUE | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
// Get the screen buffer information including size and position of window
if (!GetConsoleScreenBufferInfo(hCon, &csbiScreenInfo))
{
// Put error handling here
return 1;
}
// Calculate number of cells on screen from screen size
dwScrSize = csbiScreenInfo.dwMaximumWindowSize.X * csbiScreenInfo.dwMaximumWindowSize.Y;
// Fill the screen with the specified attribute
FillConsoleOutputAttribute(hCon, wAttributes, dwScrSize, coordStart, &dwNumWritten);
// Set attribute for newly written text
SetConsoleTextAttribute(hCon, wAttributes);
内联注释应该足以让您了解提供的文档链接的基本情况。我们使用 GetConsoleScreenBufferInfo 获取屏幕尺寸,并使用它来确定屏幕上要使用 FillConsoleOutputAttribute 更新新属性的单元格数量。然后,我们使用 SetConsoleTextAttribute 来确保打印的所有新文本与我们用于为整个控制台屏幕着色的属性相匹配。
为简洁起见,我省略了对 FillConsoleOutputAttribute 和 SetConsoleTextAttribute 调用的错误检查。我为 GetConsoleScreenBufferInfo 的错误处理添加了一个存根。我把它留作原始发帖人的练习,如果他们愿意,可以添加适当的错误处理。
我搜索了无数论坛和网站,但似乎找不到答案。我正在尝试使用 SetConsoleTextAttribute 但它只影响文本。我怎样才能像命令 color 1f
那样影响整个屏幕?我的代码是:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <wincon.h>
using namespace std;
int main()
{
SetConsoleTitle("C++ CALCULATOR"); // Title of window
int x; // Decision
int a; // First Number
int b; // Second Number
int c; // Answer
HANDLE Con;
Con = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(Con, BACKGROUND_BLUE | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
cout << "CALCULATOR" << endl << endl;
cout << "1:ADDITION" << endl << "2:SUBTRACTION" << endl << "3:MULTIPLICATION";
cout << endl << "4:DIVISION" << endl << "5:EXIT" << endl;
cin >> x;
switch (x)
{
case 1: // Addition code
cout << endl << "ADDITION" << endl << "FIRST NUMBER:";
cin >> a;
cout << endl << "SECOND NUMBER:";
cin >> b;
c = a + b;
cout << endl << "ANSWER:" << c;
break;
case 2: // Subtraction code
cout << endl << "SUBTRACTION" << endl << "FIRST NUMBER:";
cin >> a;
cout << endl << "SECOND NUMBER:";
cin >> b;
c = a - b;
cout << endl << "ANSWER:" << c;
break;
case 3: // Multiplication code
cout << endl << "MULTIPLICATION" << endl << "FIRST NUMBER:";
cin >> a;
cout << endl << "SECOND NUMBER:";
cin >> b;
c = a * b;
cout << endl << "ANSWER:" << c;
break;
case 4: // Division code
cout << endl << "DIVISION" << endl << "FIRST NUMBER:";
cin >> a;
cout << endl << "SECOND NUMBER:";
cin >> b;
c = a / b;
cout << endl << "ANSWER:" << c;
break;
case 5: // Exit code
return 0;
}
}
SetConsoleTextAttribute
更改您写入控制台的新字符的属性,但不会影响控制台的现有内容。
如果您想更改已显示在控制台上的现有角色的属性,请改用 WriteConsoleOutputAttribute
。
此解决方案依赖于这些 WinAPI 函数和结构:
- GetConsoleScreenBufferInfo 获取屏幕尺寸
- FillConsoleOutputAttribute 用属性填充屏幕
- CONSOLE_SCREEN_BUFFER_INFO 存储屏幕信息的结构
代码如下:
HANDLE hCon;
CONSOLE_SCREEN_BUFFER_INFO csbiScreenInfo;
COORD coordStart = { 0, 0 }; // Screen coordinate for upper left
DWORD dwNumWritten = 0; // Holds # of cells written to
// by FillConsoleOutputAttribute
DWORD dwScrSize;
WORD wAttributes = BACKGROUND_BLUE | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
// Get the screen buffer information including size and position of window
if (!GetConsoleScreenBufferInfo(hCon, &csbiScreenInfo))
{
// Put error handling here
return 1;
}
// Calculate number of cells on screen from screen size
dwScrSize = csbiScreenInfo.dwMaximumWindowSize.X * csbiScreenInfo.dwMaximumWindowSize.Y;
// Fill the screen with the specified attribute
FillConsoleOutputAttribute(hCon, wAttributes, dwScrSize, coordStart, &dwNumWritten);
// Set attribute for newly written text
SetConsoleTextAttribute(hCon, wAttributes);
内联注释应该足以让您了解提供的文档链接的基本情况。我们使用 GetConsoleScreenBufferInfo 获取屏幕尺寸,并使用它来确定屏幕上要使用 FillConsoleOutputAttribute 更新新属性的单元格数量。然后,我们使用 SetConsoleTextAttribute 来确保打印的所有新文本与我们用于为整个控制台屏幕着色的属性相匹配。
为简洁起见,我省略了对 FillConsoleOutputAttribute 和 SetConsoleTextAttribute 调用的错误检查。我为 GetConsoleScreenBufferInfo 的错误处理添加了一个存根。我把它留作原始发帖人的练习,如果他们愿意,可以添加适当的错误处理。