buttons.png
buttons.png

Reputation: 77

AddFontResource + SetCurrentConsoleFontEx are not changing a console font

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;

}

Upvotes: 2

Views: 653

Answers (1)

zett42
zett42

Reputation: 27756

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.

Upvotes: 5

Related Questions