使用ncurses显示wchar_t [英] Display wchar_t using ncurses
问题描述
我目前正在从事一个C ++项目,我需要在其中显示一些扩展字符(wchar_t).
i'm currently working on a C++ project in which I need to display some extended characters (wchar_t).
主要问题是,即使它在C中正常工作(使用wprintf
),也不能在使用mvwaddwstr
或waddwstr
的c ++中工作.当然,我已经将语言环境设置为:setlocale(LC_ALL, "");
,并且什么都不会显示.
The main problem is that, even if it works fine in C (using wprintf
), it doesn't work in c++ using mvwaddwstr
or waddwstr
. Of course, i've set the locale like that: setlocale(LC_ALL, "");
, and nothing is displayed.
有人以前有这个问题吗,或者对此有一个想法?
Does someone got this problem before, or has an idea about that?
谢谢.
这是代码:
struct charMap { int x; int y; wchar_t value };
int i, x, y;
wchar_t str[2];
struct charMap _charMap[2] = {
{0,0,9474}
{29, 29, 9474}
};
initscr();
setlocale(LC_ALL, "");
for (y = 0 ; y < 30 /* length */ + 2 ; y++) {
for (x = 0 ; x < 30 /* width */ + 2; x++) {
for (i = 0 ; i < 2 ; i++) {
if ((x == _charMap[i].x || _charMap[i].x == -1) &&
(y == _charMap[i].y || _charMap[i].y == -1)) {
str[0] = _charMap[i].value;
str[1] = L'\0';
mvwaddwstr(stdscr, y, x, str);
break;
}
}
}
}
refresh();
while(1);
_charMap是一个结构表,其中包含有用的值以便于进行比较(避免使用笨重的if ... else if ... else
结构). _charMap[].value
是wchar_t
,_charMap[].x
是int,例如_charMap[].y
.
_charMap is a struct table containing useful values for easy comparison (avoiding the heavy if ... else if ... else
structure). _charMap[].value
is a wchar_t
, and _charMap[].x
is an int, like _charMap[].y
.
推荐答案
您需要setlocale(LC_ALL, "")
之前进行initscr()
.
一个工作示例:
#include <ncursesw/ncurses.h>
#include <locale.h>
#include <wchar.h>
int main() {
setlocale(LC_ALL, "");
initscr();
wchar_t wstr[] = { 9474, L'\0' };
mvaddwstr(0, 0, wstr);
refresh();
getch();
endwin();
return 0;
}
这篇关于使用ncurses显示wchar_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!