AddFontResource + SetCurrentConsoleFontEx are not changing a console font

白昼怎懂夜的黑 提交于 2021-02-18 17:53:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!