cegui支持中文必须转换格式,在setText的时候用utf8格式。下面两个函数很有帮助
long atou(char* str, int ilen)
{
wchar_t *wstr = new wchar_t[4096];
memset(wstr, 0, sizeof(wchar_t) * 4096);
long lLen = MultiByteToWideChar(CP_ACP,0,str,-1,wstr,4096);
lLen = WideCharToMultiByte ( CP_UTF8, 0, wstr, -1, str, ilen, NULL, NULL );
delete[] wstr;
return lLen;
}
CEGUI::String& _AU(const char * c)
{
static CEGUI::String str;
WCHAR e;
str.clear();
int i = 0;
while(c[i])
{
if(unsigned char(c[i])>=161)
{
MultiByteToWideChar(0,0,c+i,2,&e,1);
str.append(1,e);
i+=2;
}
else
{
str.append(1,c[i]);
i++;
}
}
return str;
网上找到了常用字符串编码转换的函数,都一起放这里好了。
#include <windows.h> #include <string> /// @brief ascii和unicode之间的编码转换 size_t ascii_to_unicode(const char* src, wchar_t* dest, size_t dest_len, size_t src_len = 0) { if(src_len == 0) src_len = strlen(src); return MultiByteToWideChar(CP_ACP, 0, src, static_cast<int>(src_len), dest, static_cast<int>(dest_len)); } size_t unicode_to_ascii(const wchar_t* src, char* dest, size_t dest_len, size_t src_len = 0) { if(src_len == 0) src_len = wcslen(src); return WideCharToMultiByte(CP_ACP, 0, src, static_cast<int>(src_len), dest, static_cast<int>(dest_len), NULL, NULL); } /// @brief utf8和unicode之间的编码转换 size_t utf8_to_unicode(const unsigned char* src, wchar_t* dest, size_t dest_len, size_t src_len = 0) { if(src_len == 0) src_len = strlen((const char*)src); return MultiByteToWideChar(CP_UTF8, 0, (const char*)src, static_cast<int>(src_len), dest, static_cast<int>(dest_len)); } size_t unicode_to_utf8(const wchar_t* src, unsigned char* dest, size_t dest_len, size_t src_len = 0) { if(src_len == 0) src_len = wcslen(src); return WideCharToMultiByte(CP_UTF8, 0, src, static_cast<int>(src_len), (char*)dest, static_cast<int>(dest_len), NULL, NULL); } /// @brief ascii和utf8之间的编码转换 size_t ascii_to_utf8(const char* src, unsigned char* dest, size_t dest_len, size_t src_len = 0) { if(src_len == 0) src_len = strlen(src); std::wstring unicode(src_len, 0); unicode.resize( ascii_to_unicode(src, const_cast<wchar_t*>(unicode.data()), unicode.size(), src_len)); return unicode_to_utf8(unicode.da ta(), dest, dest_len, unicode.size()); } size_t utf8_to_ascii(const unsigned char* src, char* dest, size_t dest_len, size_t src_len = 0) { if(src_len == 0) src_len = strlen((const char*)src); std::wstring unicode(src_len * 2, 0); unicode.resize( utf8_to_unicode(src, const_cast<wchar_t*>(unicode.da ta()), unicode.size(), src_len)); return unicode_to_ascii(unicode.da ta(), dest, dest_len, unicode.size()); } /// @brief 普通字符串转宽字符串 std::wstring& to_wstring(const char* str, size_t len, std::wstring& ret) { if(len == 0) len = strlen(str); ret.resize(len); ret.resize(ascii_to_unicode(str, const_cast<wchar_t*>(ret.da ta()), ret.size(), len)); return ret; } std::wstring to_wstring(const char* str, size_t len = 0) { std::wstring ret; return to_wstring(str, len, ret); } std::wstring to_wstring(const std::string& str) { std::wstring ret; return to_wstring(str.da ta(), str.size(), ret); } std::wstring& to_wstring(const std::string& str, std::wstring& ret) { return to_wstring(str.da ta(), str.size(), ret); } /// @brief 宽字符串转普通字符串 std::string& to_string(const wchar_t* str, size_t len, std::string& ret) { if(len == 0) len = wcslen(str); ret.resize(len * 2); ret.resize(unicode_to_ascii(str, const_cast<char*>(ret.da ta()), ret.size(), len)); return ret; } std::string to_string(const wchar_t* str, size_t len) { std::string ret; return to_string(str, len, ret); } std::string to_string(const std::wstring& str) { std::string ret; return to_string(str.da ta(), str.size(), ret); } std::string& to_string(const std::wstring& str, std::string& ret) { return to_string(str.da ta(), str.size(), ret); }
}