2010-07-11 141 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