gpt4 book ai didi

c++ - 如何更改控制台字体?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:13:33 25 4
gpt4 key购买 nike

我在 Windows XP 控制台中输出 Unicode 时遇到问题。(Microsoft 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.aspxint 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 >”放入“code tag”时,带有 in <> (fcntl.h) 的部分消失了。我怎样才能把系统包含?

最佳答案

通过 Google 在此处找到这些说明:
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

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

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

关于c++ - 如何更改控制台字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3222213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com