我正在尝试使用下面指定的 unicode 特殊字符通过代码设置对话框项文本:
https://www.fileformat.info/info/unicode/char/1f310/index.htm
我一直在尝试调用 SetWindowTextW 函数,将 UTF-16(十六进制)值作为参数传递,但没有成功:
GetDlgItem(IDSETTINGS)->SetWindowTextW(_T("\uD83C\uDF10"));
当我构建我的解决方案时,我遇到了两个错误:
error C3850: '\uD83C' a universal-character-.name specified an invalid character
error C3850: '\uDF10' a universal-character-.name specified an invalid character
我将不胜感激任何形式的帮助。
最佳答案
在这种情况下,可以在 reference 中找到编译器错误 C3850 的原因。 (强调我的):
Characters represented as universal character names must represent valid Unicode code points in the range 0-10FFFF. A universal character name cannot contain a value in the Unicode surrogate range, D800-DFFF, or an encoded surrogate pair. The compiler generates the surrogate pair from a valid code point automatically.
使用 UTF-32 代码点适合我:
GetDlgItem( IDSETTINGS )->SetWindowTextW( L"\U0001F310" );
如果您确保源文件使用 Unicode 编码存储,您也可以按字面意义将字符存储在源文件中,我建议使用带 BOM 的 UTF-8。
GetDlgItem( IDSETTINGS )->SetWindowTextW( L"🌐" );
请注意,在使用 W
(Unicode) API 时,切勿使用 _T()
或 _TEXT()
宏。这些宏根据预处理器变量更改字符串文字的类型,而 Unicode API 始终需要宽字符串,这是通过对字符串文字使用 L
前缀来强制执行的。
关于c++ - 无法使用 Unicode 特殊字符设置窗口文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49127538/