c++ - 如何使用 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);
解决方案
你应该使用WriteConsoleOutputW
. CHAR_INFO 类型是属性结构(在您的情况下为颜色)和字符和宽字符的联合,所以这里没问题。
一些控制台字体不支持很多字符,请检查您的默认字体是否支持您要打印的字符。
推荐阅读
- sql - 动态转换数组元素的类型以匹配 PostgreSQL 查询中的某些表达式类型
- c# - 如何取消和暂停/恢复 Web 客户端异步下载?
- group-by - groupBy 使用 pyspark-sql-dataframe 的条件逻辑
- r - GAM 中的估计自由度是如何确定的?
- python - TypeError: read_csv() 采用 0 到 1 个位置参数,但给出了 2 个
- .net - 为什么当系统的 CPU 使用率较低时,应用程序池的 CPU 会受到限制?
- ios - 如何比较 INStartCallContactResolutionResult 对象以进行单元测试?
- html - 如何在不干扰 td 的情况下设置宽度
- jquery - 多个光谱颜色选择器
- http - 我应该在前端轮询,还是通过 websocket 发送后端?