gpt4 book ai didi

c++ - Windows CE、Unicode 和 Visual Studio 2008

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:12:48 25 4
gpt4 key购买 nike

我正在尝试编写我们现有 Windows CE 产品系列的多语言版本。我有一个 UTF8(无 BOM)编码的文件,我正在尝试读取它并将其显示在 MessageBox 中。

如果我在 CPP 文件中放置 Unicode 字符并以这种方式显示它,但有些字符不显示,我很幸运。

我试过UTF16、UTF16-LE和UTF16-BE文件,似乎都无法显示。

我已经尝试过 ifstream 和 wfstream 以及 CFile - 我也尝试过我在谷歌搜索时发现的 CStudioFile。我可以将文本保存回另一个文件并将其视为 Unicode - 我只是在我的 Windows CE 应用程序中看不到它。

注意:我编译时开启了 Unicode 字符集开关。

我还没有弄清楚正在读取文件的编码方式 - 但正如我所说,我可以将其保存回去并以 Unicode 格式读取它,但不会显示在对话框中。

感谢任何帮助\指导,以在 MessageBox 中显示 Unicode 文本。请注意,Windows CE 的 C++ SDK 非常精简,有很多新功能不可用,而且我必须在 Visual Studio 2008 中编译它。

从下面的答案中编辑代码片段。 MessageBoxW 不显示正确的文本。这可能只是模拟器上的字体问题。

CFile file(L"c:\\test\\_utf8.txt", CFile::modeRead);
int filesize = file.GetLength();
CStringA strA;
file.Read(strA.GetBuffer(filesize), filesize);
strA.ReleaseBuffer();
CStringW strW = CA2W(strA, CP_UTF8);
strW.AppendChar(0);
MessageBoxW(0, strW, 0, 0);

最佳答案

Windows 使用 UTF16-LE。使用MultiByteToWideChar(CP_UTF8, ...)UTF8转换为UTF16-LE,然后显示UTF16-LE 使用 Windows API 函数的“宽字符字符串”。

在MFC中你可以使用

CFile file(L"c:\\test\\_utf8.txt", CFile::modeRead);
int filesize = file.GetLength();
CStringA strA;
file.Read(strA.GetBuffer(filesize), filesize);
strA.ReleaseBuffer();
CStringW strW = CA2W(strA, CP_UTF8);
strW.AppendChar(0);
MessageBoxW(0, strW, 0, 0);

我不确定 WinCE 中有什么可用。要使用 C++ 标准库,请使用 std::fstream 打开,读取 std::string 并将其转换为 std::wstring

纯 API 中的另一个示例:

wchar_t* readfile(const wchar_t* filename)
{
HANDLE handle = CreateFileW(filename, GENERIC_READ, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle == INVALID_HANDLE_VALUE) return NULL;

wchar_t *wbuf = NULL;
int len = GetFileSize(handle, 0);
if (len)
{
char *buf = new char[len];
DWORD temp;
ReadFile(handle, buf, len, &temp, 0);

int wlen =
MultiByteToWideChar(CP_UTF8, 0, buf, len, 0, 0);
wbuf = new wchar_t[wlen + 1];
MultiByteToWideChar(CP_UTF8, 0, buf, len, wbuf, wlen);
wbuf[wlen] = 0;
delete[]buf;
}
CloseHandle(handle);
return wbuf;
}

...
wchar_t *wbuf = readfile(L"c:\\test\\utf8.txt");
if (wbuf)
{
MessageBoxW(0, wbuf, 0, 0);
delete[]wbuf;
}

关于c++ - Windows CE、Unicode 和 Visual Studio 2008,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38259722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com