问题描述
我正在尝试将一种控制台字体更改为一种自定义字体,但是尽管这是我试图在Internet上寻找解决方案时想到的,但是此特定代码段似乎并没有完成任何事情.通过手动安装并添加到控制台中,我只用此自定义字体测试了SetCurrentConsoleFontEx,它一直运行正常.
I'm trying to change a console font to a custom one, but this specific code piece doesn't seem to acomplish anything, even though this is what I came up while trying to find a solution around the Internet. I tested just the SetCurrentConsoleFontEx with this custom font by installing and adding it to the console with regestry by hand, and it's been functioning properly.
#include <iostream>
#include <Windows.h>
int main()
{
std::cout << "Default font" << std::endl;
system("pause");
HANDLE m_stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
AddFontResourceEx(L"Iosevka.ttf", FR_PRIVATE, 0);
SendNotifyMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
CONSOLE_FONT_INFOEX cfie;
ZeroMemory(&cfie, sizeof(cfie));
cfie.cbSize = sizeof(cfie);
cfie.dwFontSize.Y = 21;
lstrcpyW(cfie.FaceName, L"Iosevka");
SetCurrentConsoleFontEx(m_stdOut, false, &cfie);
std::cout << "Custom font" << std::endl;
RemoveFontResource(L"Iosevka.ttf");
system("pause");
return 0;
}
推荐答案
您正在使用 FR_PRIVATE
标志调用 AddFontResourceEx()
,这意味着该字体仅对您可用.过程.
You are calling AddFontResourceEx()
with FR_PRIVATE
flag, which means the font is available only to your process.
不幸的是,控制台窗口不是您的进程的一部分( GetWindowThreadProcessId()
就此而言!).它由系统进程托管(Win 7之前为"csrss.exe",此后为"conhost.exe".)
Unfortunately, the console window is not part of your process (GetWindowThreadProcessId()
lies in this regard!). It is hosted by a system process ("csrss.exe" before Win 7, "conhost.exe" since then).
要使字体对控制台可用,您必须删除 FR_PRIVATE
标志或永久安装字体.
To make the font available to the console, you have to remove the FR_PRIVATE
flag or install the font permanently.
这篇关于AddFontResource + SetCurrentConsoleFontEx不会更改控制台字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!