使用 WriteConsoleOutputAttribute 时,是什么导致控制台出现这些奇怪的颜色?

问题描述 投票:0回答:0

我目前正在尝试用颜色编码在某些位置设置带有字符的控制台窗口,以使事情变得有趣。一切都很好,很简单,直到我尝试对第 4 行的一行文本执行简单的着色操作,这导致许多奇怪的背景颜色和前景色混合在一起。这可能是什么原因?

我创建了一个简单的可重现示例:

#define UNICODE

#include <iostream>
#include <Windows.h>
#include <string>

#pragma comment(lib, "User32.lib")

using namespace std;

int main()
{
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE hStdErr = GetStdHandle(STD_ERROR_HANDLE);
    
    SMALL_RECT consoleCoords{0, 0, 79, 19};
    COORD sbSize{80, 20};
    
    if(!SetConsoleWindowInfo(hStdOut, TRUE, &consoleCoords)) /* HANDLE hConsoleOutput, BOOL bAbsolute, const SMALL_RECT* lpConsoleWindow */
    {
        wcout << L"SetConsoleWindowInfo failed. Error Code: " << GetLastError() << L'\n';
        system("pause");
        return 0;
    }
    if(!SetConsoleScreenBufferSize(hStdOut, sbSize)) /* HANDLE hConsoleOutput, COORD dwSize */
    {
        wcout << L"SetConsoleScreenBufferSize failed. Error Code: " << GetLastError() << L'\n';
        system("pause");
        return 0;
    }
    DWORD cCharsWritten{};
    DWORD attrsWritten{};
    CONSOLE_SCREEN_BUFFER_INFO bufferInfo{};
    DWORD dwConsoleSz{};
    system("pause");
    
    if(!GetConsoleScreenBufferInfo(hStdOut, &bufferInfo))
    {
        wcout << L"GetConsoleScreenBufferInfo failed. Error Code: " << GetLastError() << '\n';
        system("pause");
        return 0;
    }
    dwConsoleSz = bufferInfo.dwSize.X * bufferInfo.dwSize.Y; /* X (character cells) * Y (character cells) */
    COORD cursorHome{0, 0};
    /* Now we fill the entire screen with blanks. */
    if(!FillConsoleOutputCharacter(
            hStdOut,
            L' ',
            dwConsoleSz,
            cursorHome,
            &cCharsWritten
        )
    )
    {
        wcout << L"FillConsoleOutputCharacter failed. Error Code: " << GetLastError() << '\n';
        system("pause");
        return 0;
    }
    if(!SetConsoleCursorPosition(hStdOut, cursorHome))
        /*Research is needed regarding this function as it does not seem to move the cursor. */
    {
        wcout << L"SetConsoleCursorPosition failed. Error Code: " << GetLastError() << '\n';
        system("pause");
        return 0;
    }
    
    wchar_t topmiddlebottomRow[] = L"================";
    wchar_t separatorString[] = L"================================================================================";
    
    LPCWSTR writeRow = topmiddlebottomRow;
    LPCWSTR writeSeparator = separatorString;
    
    WORD S_attr = FOREGROUND_GREEN;
    
    if(!WriteConsoleOutputCharacterW(hStdOut, writeRow, 16, COORD{64, 0}, &cCharsWritten))
    {
        wcout << L"WriteConsoleOutputCharacterW failed. Error Code: " << GetLastError() << L'\n';
        system("pause");
        return 0;
    }
    if(!WriteConsoleOutputCharacterW(hStdOut, writeRow, 16, COORD{64, 1}, &cCharsWritten))
    {
        wcout << L"WriteConsoleOutputCharacterW failed. Error Code: " << GetLastError() << L'\n';
        system("pause");
        return 0;
    }
    if(!WriteConsoleOutputCharacterW(hStdOut, writeRow, 16, COORD{64, 2}, &cCharsWritten))
    {
        wcout << L"WriteConsoleOutputCharacterW failed. Error Code: " << GetLastError() << L'\n';
        system("pause");
        return 0;
    }
    if(!WriteConsoleOutputCharacterW(hStdOut, writeSeparator, 80, COORD{0, 4}, &cCharsWritten))
    {
        wcout << L"WriteConsoleOutputCharacterW failed. Error Code: " << GetLastError() << L'\n';
        system("pause");
        return 0;
    }
    if(!WriteConsoleOutputAttribute(hStdOut, &S_attr, 80, COORD{0, 4}, &attrsWritten))
    {
        wcout << L"WriteConsoleOutputAttribute failed. Error Code: " << GetLastError() << L'\n';
        system("pause");
        return 0;
    }
    
    
    
    while(1)
    {
    }
    
    return 0;
}

#pragma comment 预处理器行可能是不必要的——我还在学习。

c++ winapi console
© www.soinside.com 2019 - 2024. All rights reserved.