我在 Windows XP 控制台中输出 Unicode 有问题。(Microsoft Windows XP [版本 5.1.2600])第一个代码是(来自http://www.siao2.com/2008/03/18/8306597.aspx)
#include
#include
#include
int main(void) {
_setmode(_fileno(stdout), _O_U16TEXT);
wprintf(L"\x043a\x043e\x0448\x043a\x0430 \x65e5\x672c\x56fd\n");
wprintf(L"èéøÞǽлљΣæča\n");
wprintf(L"ぐႢ\n");
wprintf(L"\x3050\x10a0\n");
return 0;
}
我的代码页是 65001(CP_UTF8)。除了Ⴂ,每个字母看起来都不错。但Ⴂ 看起来像正方形。控制台的默认字体“Lucida Console”没有该字母的字体。所以,我下载了一些其他可以正确渲染Ⴂ的字体,但我无法更改(Visual Studio 2005 项目)控制台字体。
我更改了 HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe\FontName,但是当我检查 Prompt 的属性 -> 字体时,它设置为“Lucida Console”。有什么方法可以使用 API 更改控制台字体?
下一个代码是我尝试过的。但它不起作用。帮助。
#include “stdafx.h” #include "Windows.h" #包括 使用命名空间标准; // 传统智慧是迟钝的,也就是 @#%&* 是什么 _O_U16TEXT? // http://www.siao2.com/2008/03/18/8306597.aspx int main() { locale::global(locale("")); // Windows 命令提示符使用代码页 850, // 可能是为了向后兼容旧的 DOS 程序。 // Windows 命令提示符下的 Unicode(C++;.Net;Java) // http://illegalargumentexception.blogspot.com/2009/04/i18n-unicode-at-windows-command-prompt.html // 信息:SetConsoleOutputCP 仅对 Unicode 字体有效 // http://support.microsoft.com/kb/99795 // 未记录的 API:SetConsoleFont // http://cboard.cprogramming.com/windows-programming/102187-console-font-size.html typedef BOOL (WINAPI *FN_SETCONSOLEFONT)(HANDLE, DWORD); FN_SETCONSOLEFONT 设置控制台字体; HMODULE hm = GetModuleHandle(_T("KERNEL32.DLL")); SetConsoleFont = (FN_SETCONSOLEFONT) GetProcAddress(hm, "SetConsoleFont"); int fontIndex = 10; // 10 已知识别 Lucida Console(一种 Unicode 字体) BOOL bRet = SetConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE), fontIndex); // http://stackoverflow.com/questions/1922294/using-unicode-font-in-c-console-app //const UINT codePage = CP_UTF8; // 常量 UINT 代码页 = 1200; // 1200(utf-16 Unicode) SetConsoleOutputCP(codePage); wchar_t s[] = L"èéøÞǽлљΣæča\n"; int bufferSize = WideCharToMultiByte(codePage, 0, s, -1, NULL, 0, NULL, NULL); char* m = new char[bufferSize]; WideCharToMultiByte(codePage, 0, s, -1, m, bufferSize, NULL, NULL); // 0x00000459 "目标多字节代码页中不存在 Unicode 字符的映射。" wprintf(L"%S", m); // 它不起作用 wprintf(L"%s", s); // 它有点工作 // 在 L'Ⴂ' 字母之后,wcout 失败! wcout
PS:顺便说一句,当我在“代码标签”中放入“include < fcntl.h >”时,带有 <> (fcntl.h) 的部分消失了。我怎样才能把系统包括?