UTF-8 从文件到文本框 VC++ 6.0
如何让旧的 VC++ 6.0 MFC 程序在 TextBox 或 MessageBox 中读取和显示 UTF8? 最好不要破坏当前写入其中的任何文件读取和显示(相当大)。
我在 CString strStr 中读入一行,然后使用以下代码:
int nLengthNeeded = MultiByteToWideChar(CP_UTF8,0,strStr,1024,0,0);
wchar_t * pWCMessage = new wchar_t[ nLengthNeeded ];
MultiByteToWideChar(CP_UTF8,0,strStr,1024,pWCMessage,nLengthNeeded);
nLengthNeeded = MultiByteToWideChar(CP_UTF8,0,"Error Title",50,0,0);
wchar_t * pWCTitle = new wchar_t[ nLengthNeeded ];
MultiByteToWideChar(CP_UTF8,0,"Error Title",50,pWCTitle,nLengthNeeded);
MessageBoxW(NULL,pWCMessage,pWCTitle,MB_ICONINFORMATION);
仍然不确定如何将其放入文本框中,但事实证明我不需要这样做。
How do I get an old VC++ 6.0 MFC program to read and display UTF8 in a TextBox or MessageBox? Preferably without breaking any of the file reading and displaying that is currently written in there (fairly substantial).
I read a line into CString strStr, then used this code:
int nLengthNeeded = MultiByteToWideChar(CP_UTF8,0,strStr,1024,0,0);
wchar_t * pWCMessage = new wchar_t[ nLengthNeeded ];
MultiByteToWideChar(CP_UTF8,0,strStr,1024,pWCMessage,nLengthNeeded);
nLengthNeeded = MultiByteToWideChar(CP_UTF8,0,"Error Title",50,0,0);
wchar_t * pWCTitle = new wchar_t[ nLengthNeeded ];
MultiByteToWideChar(CP_UTF8,0,"Error Title",50,pWCTitle,nLengthNeeded);
MessageBoxW(NULL,pWCMessage,pWCTitle,MB_ICONINFORMATION);
Still not sure how I would get it into a textbox, but it turns out I don't need to do that anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我觉得这不会有帮助,但它是一个起点......我假设它不“正常工作”,而且我不认为你想尝试搞乱可能会出现的古怪代码页或者可能无法得到你想要的东西。
如何仅使用 MultiByteToWideChar(CP_UTF8, ...) 将其转换为 utf16,然后调用这些函数的 W 版本(或为项目定义 UNICODE/_UNICODE)。
我知道这适用于 MessageBox,而且我无法想象文本框没有 unicode 支持。
如果您需要将输出恢复为 UTF8 - 只需使用 WideCharToMultiByte()。
I feel like this won't be helpful, but it's a starting point... I'm assuming it doesn't 'just work', and I don't think you want to try to screw around with wacky code pages that may or may not get you what you want.
How about just using MultiByteToWideChar(CP_UTF8, ...) to convert it to utf16 and then calling the W versions of those functions (or defining UNICODE/_UNICODE for the project).
I know that will work for MessageBox, and I can't imagine the text box doesn't have unicode support.
If you need to get the output back to UTF8 - just use WideCharToMultiByte().
看一下:
widechartomultibyte 和 了解更多一般信息
当/如果您遇到麻烦,请确保您发布你的代码。 我已经有一段时间没有这样做了,我记得这有点棘手。
take a look at:
widechartomultibyte and for more general info
when/if you run into trouble, make sure you post your code. It's been a while since I did that and I remember it being a bit tricky.
首先使用 API MultiByteToWideChar 将 utf8 字符串转换为宽字符串,传递 CP_UTF8 作为代码页参数。
如果您的应用程序是使用定义的 _UNICODE 进行编译的,那么您现在可以将宽字符串传递到文本框。
但是,如果您的应用程序编译为 MBCS 应用程序,则必须使用 WideCharToMultiByte,传递 CP_ACP 作为代码页参数。
Convert the utf8 string first to a wide string with the API MultiByteToWideChar, passing CP_UTF8 for the codepage parameter.
If your application is compiled with _UNICODE defined, you can now pass the wide string to your textbox.
If your application however is compiled as an MBCS application, you have to convert the wide string back to MBCS with WideCharToMultiByte, passing CP_ACP as the codepage parameter.
您的应用程序是 Unicode 吗? 如果是,请回退到 Stefan 的答案:
MyDisplayableUtf16String = MultiByteToWideChar(CP_UTF8, MyUtf8String,...)
我假设它没那么简单:您的应用程序是 ANSI。
首先,您需要将字符串转换为 UTF16,如上所示。
然后存在一个瓶颈:您的 UTF8 字符串是否包含当前系统代码页(也称为“非 Unicode 应用程序的语言”)之外的字符?
将 UTF16 字符串转换为系统区域设置
如果没有,请使用(或使用 ATL/MFC 宏之一,例如
W2A(MyUtf16String)
),然后就完成了。
否则,该字符串无法转换为 ANSI,这意味着您将很难尝试在 ANSI 文本框或消息框中显示它。
在消息框中显示文本。
正如 Joe 指出的,您可以使用MessageBoxW(...,MyDisplayableUtf16String,...)
尾随的 W 表示 API 的 Unicode (UTF16) 版本。
不过,在文本框中显示字符串仍然很困难:您需要使用
CreateWindowExW()
以编程方式将文本框创建为 Unicode 窗口。 如果文本框(对话框、框架?)的父窗口不是 Unicode 窗口,我相信这将不起作用。Is your app Unicode. If yes, fallback to Stefan's answer:
MyDisplayableUtf16String = MultiByteToWideChar(CP_UTF8, MyUtf8String,...)
I'll assume it's not as simple: Your app is ANSI.
First of all, you need to convert the string to UTF16 as shown above.
Then there is a bottleneck: Does your UTF8 string contain characters ouside the current system codepage (aka "Language for non-Unicode applications")?
If not, convert the UTF16 string to the system locale using
(or use one of the ATL/MFC macros, such as
W2A(MyUtf16String)
)and you're done.
Else the string can't be converted to ANSI, which means you'll have a... ahem... hard time trying to display it in an ANSI textbox or message box.
As Joe pointed out, you can display the text in a mesage box using
MessageBoxW(...,MyDisplayableUtf16String,...)
The trailing W denotes the Unicode (UTF16) version of the API.
Displaying the string in a textbox will remain difficult though: You'd need to programmaticaly create the text box as a Unicode Window using
CreateWindowExW()
. Which I believe won't work if the parent window of the textbox (dialog,frame?) is not a Unicode window.