6

How do I make sure that the command prompt's current font is the default Raster Font, at runtime? I'm using C++ with WinApi.

For now I've used GetConsoleFontEx(); and SetConsoleFontEx();, but I haven't been able to find the right value for the CONSOLE_FONT_INFOEX's FaceName property. I found a few examples online where the font was set to Lucida and/or Consolas, but that's not what I'm looking for.

Here's a snippet of my current code:

COORD fs = {8, 8};
CONSOLE_FONT_INFOEX cfie = {0};
cfie.cbSize = sizeof(cfie);

GetCurrentConsoleFontEx(hOut, 0, &cfie);

char fn[] = "Raster"; // Not really doing anything
strcpy_s((char*)cfie.FaceName, 32, fn); // Not sure if this is right
cfie.dwFontSize.X = fs.X;
cfie.dwFontSize.Y = fs.Y;

SetCurrentConsoleFontEx(hOut, 0, &cfie);

I have tested the return value of SetCurrentConsoleFontEx(), and it's non-zero, indicating a successful call. The font does not change, though.

user4780006
  • 107
  • 1
  • 4

1 Answers1

8

Adapting the MS example of SetCurrentConsoleFontEx(), this seems to work. Note that when the cue Enter is pressed, the whole console changes font.

#include <windows.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char** args)
{ 
    CONSOLE_FONT_INFOEX cfi;
    cfi.cbSize = sizeof cfi;
    cfi.nFont = 0;
    cfi.dwFontSize.X = 0;
    cfi.dwFontSize.Y = 20;
    cfi.FontFamily = FF_DONTCARE;
    cfi.FontWeight = FW_NORMAL;
    printf("A quick brown fox jumps over the lazy dog\n");

    printf("Setting to Lucida Console: press <Enter> ");
    getchar();
    wcscpy(cfi.FaceName, L"Lucida Console");
    SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);

    printf("Setting to Consolas: press <Enter> ");
    getchar();
    wcscpy(cfi.FaceName, L"Consolas");
    SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);

    printf("Press <Enter> to exit");
    getchar();
    return 0;
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • 4
    As far as I can tell, this answers the question `How to change the font to Consolas or Lucida?`, which I explicitly mentioned to not be interested in. Instead, I'm looking for a way to change the font to the _Raster Font_, in case the user has set the default font to either Consolas or Lucida. – user4780006 Nov 28 '15 at 20:56
  • Oh, sorry, I thought the raster font meant the default Windows 7 command prompt font. What I'm talking about is the bitmap font. [Here's](http://benryves.com/tutorials/winconsole/ascii.gif) a table of the characters it has. If you open up the command prompt properties, Font tab, you'll see it listed as "Raster Fonts". – user4780006 Nov 28 '15 at 21:01
  • Try using the font "Terminal" – Weather Vane Nov 28 '15 at 21:02
  • Before the two fonts I mention were availabIe in Windows console, I used to use a raster font I installed called `OEM***` something, sorry I cannot find it right now. – Weather Vane Nov 28 '15 at 21:07
  • Hm, interesting. I noticed that if I use an obviously invalid value as `FaceName`, it'll set the font to the bitmap font. Though, when I did that, I wasn't able to effectively change the `dwFontSize` anymore. Then I noticed I hadn't set `FontFamily` nor `FontWeight`, like you did, so I tried it. Now it seems to work no matter what (invalid) font name I use. So if I set the font name to "Terminal", it seems to be working. Thank you. – user4780006 Nov 28 '15 at 21:11
  • I found `8514oem.fon` in my archive, it googles for download too. – Weather Vane Nov 28 '15 at 21:17