2010-07-11 152 views
4

我在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)消失的部分。我怎樣才能把系統包括在內?

+0

SetConsoleOutputCP(1200)返回false,因爲不接受1200作爲有效代碼頁。 – carlos 2013-08-28 20:39:23

回答

2

通過谷歌發現這裏這些指令:
http://keznews.com/3308_Adding_fonts_to_cmd_exe

缺省情況下,一個 的cmd.exe窗口上的屬性,可以選擇 或者點陣字體或龍力控制檯。 您可以通過註冊表在列表中添加其他等寬字體到 。

在註冊表編輯器,定位到

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ Console\TrueTypeFont

注意,龍力控制檯已經 此鍵與「0」的名義下。

添加一個新的叮咬值名爲 「00」(是的,這是必需的名稱) 和數據設置爲已安裝在 你的C一 等寬字體名稱:\ WINDOWS \ Fonts文件夾。在這個 的例子中,我添加了Consolas字體。它似乎需要額外的條目 名稱「000」,「0000」等。名稱如 「1」和「2」不起作用。爲了皮特的 緣故,爲什麼?

打開一個新的cmd窗口,在系統菜單上用鼠標右鍵點擊 ,選擇屬性 |字體,並且有新添加的 字體。

我這樣做是因爲我想一個更 可讀字體爲我的PowerShell 窗口,因爲我已經花了一些時間 盯着它。

來源:ferncrk。com

我按照指示使Consolas成爲我的默認字體cmd。它按預期工作。

請注意,它只接受等寬字體。

+0

感謝您更改字體。但是,我找不到任何可以顯示L'გ'(x10a0)的unicode固定字體字體。 Lucida Console將其顯示爲[]。 – 2010-07-11 12:35:31

+0

我發現了一個名爲Everson Mono的格魯吉亞語支持字體,但由於某種原因,cmd正在默默拒絕它。 – Gunslinger47 2010-07-11 20:39:09

+0

你可能有更多的運氣把這個給superuser.com。這不是編程問題,而是Windows配置的問題。 – Gunslinger47 2010-07-12 21:31:44