如何更改控制台字体?

发布于 2024-09-09 07:12:51 字数 2794 浏览 8 评论 0原文

我在 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)。除了Ⴂ,每个字母看起来都不错。但Ⴂ看起来像正方形。 控制台的默认字体“Lucida Console”没有该字母的字体。 因此,我下载了一些其他可以正确渲染Ⴂ的字体,但我无法更改(Visual Studio 2005项目)控制台字体。

我更改了 HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe\FontName,但是当我检查 Prompt 的属性时 ->字体,设置为“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:顺便说一句,当我输入“include”时在“代码标签”中,带有<>的部分(fcntl.h)消失了。我怎样才能把系统包括在内?

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;
}

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.

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, when I put "include < fcntl.h >" in "code tag", the part with in <> (fcntl.h) disappeared. How can I put system include?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撑一把青伞 2024-09-16 07:12:51

通过 Google 在这里找到了这些说明:
http://keznews.com/3308_Adding_fonts_to_cmd_exe

默认情况下,a 上的属性
cmd.exe 窗口允许您选择
光栅字体或 Lucida 控制台。
您可以添加其他等宽字体
通过注册表获取列表。

在 regedit 中,导航至

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
NT\当前版本\
控制台\TrueTypeFont

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

使用名称添加新的字符串值
“00”(是的,这是必需的名称)
并将数据设置为a的名称
等宽字体已安装在
您的 C:\Windows\Fonts 文件夹。在这个
例如,我添加了 Consolas 字体。它
似乎需要额外的条目
名称“000”、“0000”等。名称如
“1”和“2”不起作用。对于皮特来说
清酒,为什么?

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

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

来源:ferncrk.com

我按照说明将 Consolas 设置为 cmd 的默认字体。它按预期工作。

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

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

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.

In regedit, navigate to

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

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

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?

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

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

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

Note that it will only accept monospaced fonts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文