在使用webview_window显示HTML时遇到显示中文乱码的问题,原因是vscode的编码方式为UTF-8,而vs的编码方式为gb2312,因此在vscode中传入显示参数,通过channel调用方法显示时因为编码方式的不同导致中文显示乱码,因此想要支持中文显示要对webview_window插件修改,增加编码格式转换的方法。
std::string utf8_to_gb2312(const char* utf8) {
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
std::string strTemp = str;
if (wstr) delete[] wstr;
if (str) delete[] str;
return strTemp;
}
同时webview2的NavigateToString方法接收wchar_t*的参数,而传入的参数为string,因此还需

在使用webview_window显示HTML时遇到由于编码不一致(vscode为UTF-8,VS为GB2312)导致的中文乱码。为支持中文显示,需要修改webview_window插件以增加编码转换方法,并且处理webview2的NavigateToString方法中字符串到wchar_t*的转换。通过这些调整,成功解决了乱码问题。
最低0.47元/天 解锁文章
587

被折叠的 条评论
为什么被折叠?



