Search code examples
c++ccmdwindowwindows-console

I want to change Cmd Font Style


I want to Change Cmd Font with C - Coding.

But I don't know how to change it.

I want to change Basic font -> Terminal Font.

This is my code

CONSOLE_FONT_INFOEX cfi;
cfi.cbSize = sizeof cfi;
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = 16;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
wcscpy_s(cfi.FaceName,9, L"Terminal");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);

My Development Environment is on Windows 10.


Solution

  • The problem with the SetCurrentConsoleFontEx() function, is that the width of the font is not optional. You have to use a value which is consistent with the Y size and supported by the chosen font.

    For Terminal, the following should work:

    cfi.dwFontSize.X = 12;
    cfi.dwFontSize.Y = 16;
    

    If you want to check the font size available, you can enumerate the fonts. For example, with this small code:

    // callback to display some infos about one font 
    int CALLBACK logfont(_In_ const LOGFONT    *lplf, 
        _In_ const TEXTMETRIC *lptm,
        _In_       DWORD      dwType,
        _In_       LPARAM     lpData
        )
    {
        wcout << L"Font " << (wchar_t*)lplf->lfFaceName << L" " << lplf->lfHeight<<L" "<<lplf->lfWidth <<endl;
        return 1;
    }
    
    // this callback is then used in a statement like:  
    EnumFonts(GetDC((HWND)GetStdHandle(STD_OUTPUT_HANDLE)),L"Terminal", logfont, NULL);
    

    For more in-depth infos about installed fonts, this MSDN article may interest you.