首页 > 解决方案 > 如何使用 WriteConsoleOutput 编写 UNICODE 或扩展 ASCII

问题描述

仅使用 ASCII 字符时,所有字符和给定的颜色值都会正确显示,但是,每当我使用扩展 ASCII 或 UNICODE 字符时,我都会收到此错误消息(并且我的 g++ 编译器表明问题出在非 ASCII 字符上):

warning: multi-character character constant [-Wmultichar]
error: narrowing conversion of '14849683' from 'int' to 'WCHAR' {aka 'wchar_t'} inside { } [-Wnarrowing]

-lgdi32在编译命令中使用该参数。

无论我使用WriteConsoleOutputA还是WriteConsoleOutputW看起来都没有任何区别。

我相信 CHAR_INFO 数组字符宽度默认为 8 位,所以我认为应该更改它,但我在 Windows 控制台 API 文档中找不到任何相关信息。

需要明确的是,所有 # 字符看起来都是正确的,但是如果我使用以下字符:░ 或 ┐,我会收到上述错误消息。

CHAR_INFO map[400] = {
            '#', 0x002C, '#', 0x002C, '#', 0x002C, '#', 0x002C, '#', 0x002C,'#', 0x002C, '#', 0x002C, '#', 0x002C, '#', 0x002C, '#', 0x002C,
            '#', 0x002C, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', 0x002C,
            '#', 0x002C, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', 0x002C,
            '#', 0x002C, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', 0x002C,
            '#', 0x002C, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', 0x002C,
            '#', 0x002C, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', 0x002C,
            '#', 0x002C, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', 0x002C,
            '#', 0x002C, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', 0x002C,
            '#', 0x002C, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', 0x002C,
            '#', 0x002C, '#', 0x002C, '#', 0x002C, '#', 0x002C, '#', 0x002C,'#', 0x002C, '#', 0x002C, '#', 0x002C, '#', 0x002C, '#', 0x002C
        };


        COORD coordinateBufferSize;
        coordinateBufferSize.Y = 10;
        coordinateBufferSize.X = 10;

        COORD topLeftCoordinate;
        topLeftCoordinate.Y = 0;
        topLeftCoordinate.X = 0;

        PSMALL_RECT srcWriteRect;
        srcWriteRect->Top = 10;         // Number of rows to the top
        srcWriteRect->Left = 10;        // Numbers of columns to the side
        srcWriteRect->Bottom = 19;
        srcWriteRect->Right = 19;


        HANDLE oldScreenBuffer, newScreenBuffer;


        oldScreenBuffer = GetStdHandle(STD_OUTPUT_HANDLE);
        newScreenBuffer = CreateConsoleScreenBuffer(
            GENERIC_READ | GENERIC_WRITE,
            FILE_SHARE_READ | FILE_SHARE_WRITE,
            NULL,
            CONSOLE_TEXTMODE_BUFFER,
            NULL);

        BOOL succes = WriteConsoleOutputA(
            newScreenBuffer,                    // The new ScreenBuffer
            map,                                // The char array we want to display
            coordinateBufferSize,               // 
            topLeftCoordinate,
            srcWriteRect
        );

        SetConsoleActiveScreenBuffer(newScreenBuffer);

标签: c++windowsunicodeconsoleg++

解决方案


你应该使用WriteConsoleOutputW. CHAR_INFO 类型是属性结构(在您的情况下为颜色)和字符和宽字符的联合,所以这里没问题。

一些控制台字体不支持很多字符,请检查您的默认字体是否支持您要打印的字符。


推荐阅读