如何更改控制台字体? [英] How can I change console font?

查看:405
本文介绍了如何更改控制台字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows XP控制台中有输出Unicode的问题。
(Microsoft Windows XP [版本5.1.2600])
第一个代码是(从 http://www.siao2.com/2008/03/18/8306597.aspx

I have a problem with output Unicode in Windows XP console. (Microsoft Windows XP [Version 5.1.2600]) First code is that(from 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项目)控制台字体。


My codepage is 65001(CP_UTF8). Excep Ⴂ, every letter look good. But Ⴂ is look like square. Console's default font 'Lucida Console' doesn't have font for that letter. So, I downloaded some other font which can render Ⴂ correcly, but I cannot change (Visual Studio 2005 project) console font.

我更改了HKEY_CURRENT_USER \\ Console \%SystemRoot%_system32_cmd.exe\FontName,但当我检查提示的属性 - >字体,它设置为Lucida控制台。
有什么方法可以用API改变控制台字体?

I changed HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe\FontName, but when I check Prompt's properties -> Font, it set as 'Lucida Console'. Is there any way to change console font with API?

下一个代码是我试过的。但它不工作。帮助。

The next code is what I tried. But it doesn't work. Help.


#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:BTW,当我在代码标签中输入include< fcntl.h> ;>(fcntl.h)消失。如何将系统include?

PS : BTW, when I put "include < fcntl.h >" in "code tag", the part with in <> (fcntl.h) disappeared. How can I put system include?

推荐答案

通过Google在这里找到这些说明:

http://keznews.com/3308_Adding_fonts_to_cmd_exe

Found these instructions through Google here:
http://keznews.com/3308_Adding_fonts_to_cmd_exe


默认情况下,
cmd.exe窗口中的属性允许您选择
Raster Fonts或Lucida Console。
您可以通过注册表将其他等宽字体添加到
列表中。

Be default, the properties on a cmd.exe window allow you to select either Raster Fonts or Lucida Console. You can add other monospace fonts to the list via the registry.

在regedit中,导航到

In regedit, navigate to

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

请注意,Lucida Console在此键下已有
,名称为0。

Notice that Lucida Console is already under this key with a name of "0".

新的sting值名为
00(yep,这是必需的名称)
并将数据设置为已经安装在
中的
等宽字体的名称您的C: \Windows \字体文件夹。在这个
示例中,我添加了Consolas字体。它
似乎额外的条目需要
名称000,0000等。诸如
1和2的名称不起作用。对于Pete的
,为什么?

Add a new sting value with the name "00" (yep, that's the required name) and set the data to the name of a monospace font already installed in your C:\Windows\Fonts folder. In this example, I added the Consolas font. It seems that additional entries require names "000", "0000", etc. Names like "1" and "2" don't work. For Pete's sake, why?

打开一个新的cmd窗口,右键单击系统菜单上的
,选择属性
|字体和新添加的
字体。

Open up a new cmd window, right-click on the system menu, select Properties | Font and there is the newly added font.

我这样做是因为我想要一个更多的
可读字体为我的PowerShell
窗口,因为我已经花了一些
的时间盯着它。

I did this because I wanted a more readable font for my PowerShell window, since I've been spending some time staring at it.

source:ferncrk.com

source: ferncrk.com

我按照说明,并使Consolas我的默认字体为cmd。

I followed the instructions and made Consolas my default font for cmd. It worked as expected.

请注意,它只接受等宽字体。

Note that it will only accept monospaced fonts.

这篇关于如何更改控制台字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆