How to use SetConsoleTextAttribute C++

时间:2015-10-30 23:28:24

标签: winapi console-application

I have searched countless forums and websites but I can't seem to find the answer. I'm trying to use SetConsoleTextAttribute but it only affects the text. How can I affect the whole screen like the command color 1f would? My code is: #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; } }

2 个答案:

答案 0 :(得分:2)

此解决方案依赖于这些WinAPI函数和结构:

代码如下:

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 的错误处理设置了一个存根。我把它留作原始海报的练习,如果他们愿意,可以添加适当的错误处理。

答案 1 :(得分:1)

SetConsoleTextAttribute changes the attribute for new characters that you write to the console, but doesn't affect existing contents of the console. If you want to change the attributes for existing characters already being displayed on the console, use WriteConsoleOutputAttribute instead.