簡體   English   中英

如何使用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;


    }

}

該解決方案依賴於以下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來確保所打印的所有新文本與我們用來給整個控制台屏幕着色的屬性相匹配。

為簡便起見,我省略了對FillConsoleOutputAttributeSetConsoleTextAttribute的調用的錯誤檢查。 我將存根用於GetConsoleScreenBufferInfo的錯誤處理。 我將其作為原始海報的練習,如果他們願意的話,添加適當的錯誤處理。

SetConsoleTextAttribute更改您寫入控制台的新字符的屬性,但不影響控制台的現有內容。

如果要更改控制台上已經顯示的現有字符的屬性,請改用WriteConsoleOutputAttribute

暫無
暫無

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

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