问题
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;
}
回答1:
You are calling AddFontResourceEx()
with FR_PRIVATE
flag, which means the font is available only to your process.
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).
See: Windows Command-Line: Inside the Windows Console
To make the font available to the console, you have to remove the FR_PRIVATE
flag or install the font permanently.
来源:https://stackoverflow.com/questions/53904630/addfontresource-setcurrentconsolefontex-are-not-changing-a-console-font