![](/img/trans.png)
[英]Writing unicode(?) character directly from source code to WriteConsoleOutput
[英]How to write UNICODE or Extended ASCII with WriteConsoleOutput
仅使用 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);
您应该使用WriteConsoleOutputW
。 CHAR_INFO 类型是属性结构(在您的情况下为颜色)和字符和宽字符的联合,所以这里没问题。
某些控制台 fonts 不支持很多字符,请检查您的默认字体是否支持您要打印的字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.