WinAPI 和 UTF-8 支持

2022-01-12 00:00:00 utf-8 winapi localization c++ mfc

关于 UTF-8 支持和各种 Win32 API 的快速问题.

Quick question regarding UTF-8 support and various Win32 API's.

在典型的 C++ MFC 项目中,MessageBox() 是否可以显示 UTF-8 编码的字符串?

In a typical C++ MFC project, is it possible for MessageBox() to display a UTF-8 encoded string?

谢谢,安德鲁

推荐答案

快速回答:没有.

更长的答案:如果字符串只包含常规的 ANSI 字符,例如美国英语,它将起作用,因为这些字符代码在 UTF-8 和 ANSI 中是相同的.

Longer answer: It'll work if the string only contains regular ANSI characters, e.g US English, since these character codes are the same in UTF-8 and ANSI.

如果包含非 ANSI 字符或任何双字节编码字符,则需要使用 MultiByteToWideChar 和 CP_UTF8 转换为 Unicode-16.您的程序还需要使用定义的 UNICODE 进行编译,或者您可以使用W"API 调用 - 例如消息框W.

If non-ANSI characters are included, or any double-byte encoded characters, you'll need to transform to Unicode-16 using MultiByteToWideChar with CP_UTF8. Your program will also need to be compiled with UNICODE defined, or you can use the 'W' API calls - e.g. MessageBoxW.

(请注意,采用文本参数的函数,例如 MessageBox、CreateWindow 映射到A"或W"版本,具体取决于是否定义了 UNICODE).

(Note that functions taking a text argument such as MessageBox, CreateWindow map to either 'A' or 'W' versions depending on whether UNICODE is defined).

这也可能有用;

http://www.joelonsoftware.com/articles/Unicode.html

相关文章