DCSIMG
Changing Console Fonts - Pavel's Blog
Sign in | Join | Help

Pavel's Blog

Pavel is a software guy that is interested in almost everything
software related... way too much for too little time

Changing Console Fonts

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!

Comments List

# Webmaster Crap &raquo; Blog Archive &raquo; Changing Console Fonts - Pavel&#39;s Blog

Published at Saturday, July 25, 2009 2:30 AM by Webmaster Crap » Blog Archive » Changing Console Fonts - Pavel's Blog  

Pingback from  Webmaster Crap  &raquo; Blog Archive   &raquo; Changing Console Fonts - Pavel&#39;s Blog

# re: Changing Console Fonts

Published at Monday, November 29, 2010 12:27 AM by mammad  

hi :)

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

# re: Changing Console Fonts

Published at Wednesday, January 12, 2011 2:49 AM by Rubens Farias  

Wonderful approach, congratulations!

# re: Changing Console Fonts

Published at Tuesday, March 01, 2011 2:53 PM by C0n_z0L  

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 !

# possible to get/set console font size in c# .net? - Programmers Goodies

Published at Saturday, July 16, 2011 7:05 PM by possible to get/set console font size in c# .net? - Programmers Goodies  

Pingback from  possible to get/set console font size in c# .net? - Programmers Goodies

Leave a Comment

(required) 
(
required
)
 
(optional)
(required) 

Enter the numbers above: