我在Windows XP控制台输出Unicode有问题。 (微软的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)。 Excepგ,每封信看起来不错。但是У看起来像方形。 控制台的默认字体'Lucida Console'没有该字母的字体。 因此,我下载了一些其他字体可以呈现正确的,但我不能改变(Visual Studio 2005项目)控制台字体。
我更改了HKEY_CURRENT_USER \ Console \%SystemRoot%_system32_cmd.exe \ FontName,但是当我检查提示的属性 - >字体时,它设置为'Lucida Console'。 有什么方法可以通过API更改控制台字体?
下一个代码是我试过的。但它不起作用。帮帮我。
#include "stdafx.h" #include "Windows.h" #include using namespace std; // Conventional wisdom is retarded, aka What the @#%&* is _O_U16TEXT? // http://www.siao2.com/2008/03/18/8306597.aspx int main() { locale::global(locale("")); // Windows Command Prompt use code page 850, // probably for backwards compatibility with old DOS programs. // Unicode at the Windows command prompt (C++; .Net; Java) // http://illegalargumentexception.blogspot.com/2009/04/i18n-unicode-at-windows-command-prompt.html // INFO: SetConsoleOutputCP Only Effective with Unicode Fonts // http://support.microsoft.com/kb/99795 // Undocumented API : SetConsoleFont // http://cboard.cprogramming.com/windows-programming/102187-console-font-size.html typedef BOOL (WINAPI *FN_SETCONSOLEFONT)(HANDLE, DWORD); FN_SETCONSOLEFONT SetConsoleFont; HMODULE hm = GetModuleHandle(_T("KERNEL32.DLL")); SetConsoleFont = (FN_SETCONSOLEFONT) GetProcAddress(hm, "SetConsoleFont"); int fontIndex = 10; // 10 is known to identify Lucida Console (a Unicode font) 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; // const UINT codePage = 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 "No mapping for the Unicode character exists in the target multi-byte code page." wprintf(L"%S", m); // it doesn't work wprintf(L"%s", s); // it work a bit // after L'Ⴂ' letter, wcout failed! wcout
PS:顺便说一句,当我把 “包括< fcntl.h>函数” 中的 “代码标签”,用在<>(fcntl.h)消失的部分。我怎样才能把系统包括在内?
SetConsoleOutputCP(1200)返回false,因为不接受1200作为有效代码页。 – carlos 2013-08-28 20:39:23