我在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