3

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.

Wandering Fool
  • 2,170
  • 3
  • 18
  • 48
LaserFrame
  • 33
  • 5
  • Works fine. The default is already Terminal, be sure that you can see the differences. Others are "Consolas" and "Lucida Console". Don't lie to wcscpy_s(). – Hans Passant Aug 09 '15 at 14:53

1 Answers1

1

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.

Wandering Fool
  • 2,170
  • 3
  • 18
  • 48
Christophe
  • 68,716
  • 7
  • 72
  • 138