Changing Console Fonts

July 23, 2009

7 comments

The other day I created a Console application and changed some colors and sizes. Then I needed to change the font (programmatically). I looked at the System.Console class – nothing. Maybe they just forgot to wrap it up, I thought, and turned to the Win32 API functions for the Console. Guess what? Nothing. Well, almost nothing. There’s a new function (starting with Vista) called SetCurrentConsoleFontEx, but it’s almost useless. It requires a console font index (which is supposed to be from a console font table, which no documented API reveals), and it doesn’t seem to work quite as expected. The font size is given as a COORD structure (in “logical” units, where “logical” is not explained), and even then, only certain sizes actually work, with no indication why).

All this can be done pretty easily through a “shortcut” to the console app (IShellLink), but I needed something more dynamic, to be available at runtime.

This felt really wrong. It seems that some functions are missing. A quick google and some depends.exe reveals that there are actually a few functions that can help in kernel32.dll, namely SetConsoleFont, GetConsoleFontInfo and GetNumberOfConsoleFonts – the catch: undocumented.

With the google help and some investigating I was able to get the prototypes, and they seem to work from XP all the way to Windows 7 in much the same way. At least there’s a way to enumerate those fonts and change to the one you like! I also found another undocumented function, SetConsoleIcon, to change the icon. A bonus!

The prototypes are:

typedef struct _CONSOLE_FONT {

   DWORD index;

   COORD dim;

} CONSOLE_FONT;

 

BOOL WINAPI SetConsoleFont(HANDLE hOutput, DWORD fontIndex);

BOOL WINAPI GetConsoleFontInfo(HANDLE hOutput, BOOL bMaximize, DWORD numFonts, CONSOLE_FONT* info);

DWORD WINAPI GetNumberOfConsoleFonts();

BOOL WINAPI SetConsoleIcon(HICON hIcon);

I’ve created two files ConsoleFont.h/cpp that use those prototypes to make the changes.

I’ve also created a managed wrapper (ConsoleHelper.cs) that can be used from managed code, like so:

static void Main(string[] args) {

   var fonts = ConsoleHelper.ConsoleFonts;

   for(int f = 0; f < fonts.Length; f++)

      Console.WriteLine("{0}: X={1}, Y={2}",

         fonts[f].Index, fonts[f].SizeX, fonts[f].SizeY);

   ConsoleHelper.SetConsoleFont(5);

   ConsoleHelper.SetConsoleIcon(SystemIcons.Information);

}

You may use these at your own risk, as these are undocumented functions, and can be changed in the future by Microsoft, although I believe they will eventually be documented.

There are still things missing. That mysterious console font table – I didn’t find a function that can be used to add entries to it. Perhaps it’s lurking in the registry somewhere. There’s no (exported) AddConsoleFont or something like that.

Happy fonting!

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

7 comments

  1. mammadNovember 29, 2010 ב 00:27

    hi :)
    Thanks For Your Topic,I Need To Changing Console Size Font Do Can Help Me?

    Reply
  2. Rubens FariasJanuary 12, 2011 ב 02:49

    Wonderful approach, congratulations!

    Reply
  3. C0n_z0LMarch 1, 2011 ב 14:53

    You are very close to the truth – the “mysterious console font table” you are talking about is a number of entries in the registry, showing how many and which fonts are available for the console:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont

    You can add or remove fonts from it and it will be taken into account when you start the console again. Perhaps you will need to restart the whole system before that !
    You will need a little more info to make it right, so Google and read. I made it already and it works, but be carefull – you mess with the registry in a first place !

    P.S. These function prototypes you made are a “must have” for every console developer ! I’ve been looking for the syntax of undocumented console functions for days now and it is just a God bless you are posting it. Thank you VERY MUCH !

    Reply
  4. SeánOctober 5, 2012 ב 18:34

    Hi Pavel, I found your header file and C++ code for the function SetConsoleFont() which is exactly what I am looking for. To set console from font to Lucida Console from within a program. BUT am using C, not cpp.
    Do you have a straight C language version of your header and code for SetConsoleFont() ?
    If I was better experienced in C I should be able to make a translation myself no doubt, but at this time am not that far advanced in programming.

    Thanks, Sean

    Reply
  5. pavelyOctober 5, 2012 ב 22:20

    Hi Sean,

    The actual functions are there – they are C functions from the Windows API. Just #include and use the functions directly.

    Reply
  6. wqhbtbh@gmail.comAugust 17, 2013 ב 06:22

    “The bylaw refers only into the composition of the establishing, not the cleanliness or affliction,” she says.

    Reply
  7. ksl060311@gmail.comAugust 19, 2013 ב 23:47

    He broke zero cost from your restraints and lunged in a stability guard right before nearby police utilized capsicum spray and batons to restrain him.
    ����ͥ� ؔ�� ��� �Lؔ�� http://www.paris-sagan-hotels.com/chanelwallets.html

    Reply