Set font text in console application
-
Friday, January 21, 2011 2:55 PM
All Replies
-
Friday, January 21, 2011 3:16 PM
Hello,
you may do it by SetCurrentConsoleFontEx .
Nicolò Zilio
Personal blog: http://nicolozilio.wordpress.com/(With technical articles)
Personal Web site: http://nicolozilio.altervista.org/- Marked As Answer by Ángel Manuel García Carmona Friday, January 21, 2011 3:50 PM
- Unmarked As Answer by Ángel Manuel García Carmona Sunday, January 23, 2011 9:29 AM
-
Friday, January 21, 2011 3:21 PM
-
Friday, January 21, 2011 3:39 PM
I've just fount this forum thread shows how to use this function.
However, this is the code:
PCONSOLE_FONT_INFOEX lpConsoleCurrentFontEx = new CONSOLE_FONT_INFOEX; lpConsoleCurrentFontEx->cbSize = sizeof(CONSOLE_FONT_INFOEX); GetCurrentConsoleFontEx(GetStdHandle (STD_OUTPUT_HANDLE), NULL, lpConsoleCurrentFontEx); //retrive all console font informations swprintf_s(lpConsoleCurrentFontEx->FaceName, L"Lucida Console"); SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), NULL, lpConsoleCurrentFontEx); delete lpConsoleCurrentFontEx;
Nicolò Zilio
Personal blog: http://nicolozilio.wordpress.com/(With technical articles)
Personal Web site: http://nicolozilio.altervista.org/- Marked As Answer by Ángel Manuel García Carmona Friday, January 21, 2011 3:50 PM
- Unmarked As Answer by Ángel Manuel García Carmona Sunday, January 23, 2011 9:30 AM
-
Friday, January 21, 2011 3:49 PM>you may do it by SetCurrentConsoleFontEx .
Note that the minimum Windows version supported is Vista.
- Wayne -
Friday, January 21, 2011 3:56 PM
On 21/01/2011 16:39, Nicolò Zilio wrote:
[code]
PCONSOLE_FONT_INFOEX lpConsoleCurrentFontEx = new CONSOLE_FONT_INFOEX;
As a side note: this is a C#/Java way of doing things.
In C++, I'd suggest to just create CONSOLE_FONT_INFOEX on the stack (instead of using an heap allocation):
CONSOLE_FONT_INFOEX fontInfo = { 0 };
fontInfo.cbSize = sizeof(fontInfo);
> delete lpConsoleCurrentFontEx;No needed when allocated on stack.
Giovanni
-
Sunday, January 23, 2011 12:51 PM
-
Tuesday, January 25, 2011 7:48 AMÁngel Manuel García Carmona wrote:>>Hello, these code lines doesn't work. For that the application will be>in the font indicated in the code, i must to change the properties when>i want to set some code lines to show the text in a font without check>the properties.Those codes lines DO woek.Do you understand that the entire command window has to be in the samefont? You can't mix multiple fonts in a command window. When you changethe font, the entire window changes.--Tim Roberts, timr@probo.comProvidenza & Boekelheide, Inc.
Tim Roberts, DDK MVP -
Tuesday, January 25, 2011 2:46 PM
-
Thursday, January 27, 2011 3:43 AMModerator
Hi Angel,
Please perform the following steps to set font text in console application.
1.To create a Win32 console application
- Follow the instructions in the help topic Creating a Project with a Visual C++ Application Wizard.
- In the New Project dialog box, select Win32 Console Project in the Templates pane to open the wizard.
3. Click Finish to close the wizard, and your newly created project opens in Solution Explorer.
2.Add the following code to your console application:
#include "stdafx.h" #include <windows.h> #include <iostream> using namespace std ; int _tmain(int argc, _TCHAR* argv[]) { // Obtain the Console handle HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Obtain the Console handle PCONSOLE_FONT_INFOEX lpConsoleCurrentFontEx = new CONSOLE_FONT_INFOEX() ; // set the size of the CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx->cbSize = sizeof(CONSOLE_FONT_INFOEX); // get the current value GetCurrentConsoleFontEx(hConsole,0,lpConsoleCurrentFontEx); // set size to be 8x18, the default size is 8x16 lpConsoleCurrentFontEx->dwFontSize.X = 8; lpConsoleCurrentFontEx->dwFontSize.Y = 16; // submit the settings SetCurrentConsoleFontEx(hConsole,0,lpConsoleCurrentFontEx); cout<<"Hello"<<endl; cin.get(); // wait return 0; }
NOTE: dwFontSize.X and dwFontSize.Y should be set specific size defined in console properties.(generally , size should be 3x5 ; 5x8 ;6x12; 8x16; 8x18; 10x20)
Best regards,
Lucy
Lucy Liu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
Thursday, January 27, 2011 6:31 AM
Hi Angel,
Please perform the following steps to set font text in console application.
1.To create a Win32 console application
- Follow the instructions in the help topic Creating a Project with a Visual C++ Application Wizard.
- In the New Project dialog box, select Win32 Console Project in the Templates pane to open the wizard.
3. Click Finish to close the wizard, and your newly created project opens in Solution Explorer.
2.Add the following code to your console application:
#include "stdafx.h" #include <windows.h> #include <iostream> using namespace std ; int _tmain(int argc, _TCHAR* argv[]) { // Obtain the Console handle HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Obtain the Console handle PCONSOLE_FONT_INFOEX lpConsoleCurrentFontEx = new CONSOLE_FONT_INFOEX() ; // set the size of the CONSOLE_FONT_INFOEX lpConsoleCurrentFontEx->cbSize = sizeof(CONSOLE_FONT_INFOEX); // get the current value GetCurrentConsoleFontEx(hConsole,0,lpConsoleCurrentFontEx); // set size to be 8x18, the default size is 8x16 lpConsoleCurrentFontEx->dwFontSize.X = 8; lpConsoleCurrentFontEx->dwFontSize.Y = 16; // submit the settings SetCurrentConsoleFontEx(hConsole,0,lpConsoleCurrentFontEx); cout<<"Hello"<<endl; cin.get(); // wait return 0; }
NOTE: dwFontSize.X and dwFontSize.Y should be set specific size defined in console properties.(generally , size should be 3x5 ; 5x8 ;6x12; 8x16; 8x18; 10x20)
Best regards,
Lucy
Lucy Liu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Hello, thanks first but in this code isn¡t setted the font. How to set the font type, for example Lucida console?
Regards
Ángel Manuel
-
Thursday, January 27, 2011 7:54 PM
>Hello, thanks first but in this code isn¡t setted the font.
>How to set the font type, for example Lucida console?
Since Lucy's example doesn't attempt to change the font
used, it's not surprising if it doesn't do so. It doesn't
set either the console font index or the font name in the
CONSOLE_FONT_INFOEX struct. You should use your initiative
and provide the missing data in the struct yourself.
I don't have Vista or W7 installed on this PC so can't test
the SetCurrentConsoleFontEx function at present. Note these
considerations and alternatives:
(1) As Tim pointed out, there can be only one font set at
any give time in a console window. As soon as the font is
changed *all* characters on the console display will be
displayed using *that* font. One consequence of this is that
when you change the font in your program and run it from a
command line the font you choose will be used while your
program is still active, but as soon as your program ends
and control is returned to the command line processor the
font may get reset to the default. When this happens, all
characters on the screen will then be changed to those from
the default font. Any special characters you used from the
font setting in your app will disappear immediately. This
may create the impression that the font wasn't changed
when in fact it was - so it's imperative that your program
pause before ending.
(2) The simplest and surest way to get the Lucida font for
your app is to create a shortcut for your program. Set the
font to Lucida in the Properties for the *shortcut* (*.lnk)
file. Then always start your program using the shortcut rather
than the .exe itself. Then no manual setting of the font for
the console window will be needed each time you run it. It
will use the font desired automatically. (But remember my
point (1) above.)
(3) As previously noted, the SetCurrentConsoleFontEx function
is only supported in Vista and above. So it can't be used
under XP. There are *undocumented* features of the Win32 API
which work to change the font in XP. These are discussed here:
Changing Console Fonts
http://blogs.microsoft.co.il/blogs/pavely/archive/2009/07/23/changing-console-fonts.aspx
As you can see, this whole aspect of changing fonts in a
console window has not been well supported under any
version of Windows.
Here's an example of setting the font using the functions
from that blog. It uses font index 11 which on my XP SP3
system sets it to Lucida. Your results may vary, as the
installed console fonts may be different and hence so may
be the index. My system reports 12 in nFonts.
Be sure to make ConsoleFont.cpp from the blog one of the
Source Files in your project.
#include "stdafx.h"
#include <windows.h>
#include <conio.h>
#include <io.h>
#include <fcntl.h>
#include "ConsoleFont.h"
int _tmain(int argc, _TCHAR* argv[])
{
DWORD nFonts = GetNumberOfConsoleFonts();
printf("There are %lu console fonts.\n", nFonts);
_setmode(_fileno(stdout), _O_U16TEXT);
wprintf(L"\u20AC\x043e\x0448\x043a\x0430 \x65e5\x672c\x56fd\n");
SetConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE), 11);
wprintf(L"\u20AC\x043e\x0448\x043a\x0430 \x65e5\x672c\x56fd\n");
_getch();
return 0;
}
E&OE
As mentioned, these are *undocumented* and hence are not
*officially* supported. They may work differently on
different versions of Windows. In order of preference,
you should go with:
(1) A shortcut (*.lnk)
(2) SetCurrentConsoleFontEx (which *is* officially supported)
(3) The undocumented features from the Blog. I would advise
against this choice if you plan on distributing your app widely.
- Wayne- Marked As Answer by Ángel Manuel García Carmona Sunday, May 01, 2011 1:57 PM
-
Thursday, January 27, 2011 8:15 PM
Here's a small program which may help identify the font
number for Lucida. *At the first pause*, use the console
window properties to *manually* select Lucida. Then press
the space bar to see the font number. You should see
output such as:
default font number 9
Manually set font property to Lucida before continuing...
current font number 10
On my system (XP SP3), font number 10 gives Lucida normal
and number 11 gives Lucida bold.
#include "stdafx.h"
#include <windows.h>
#include <conio.h>
int _tmain(int argc, _TCHAR* argv[])
{
CONSOLE_FONT_INFO cfo;
GetCurrentConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE), FALSE,
&cfo);
printf("default font number %lu\n", cfo.nFont);
printf("Manually set font property to Lucida before continuing...\n");
_getch();
GetCurrentConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE), FALSE,
&cfo);
printf("current font number %lu\n", cfo.nFont);
_getch();
return 0;
}
E&OE
- Wayne- Marked As Answer by Ángel Manuel García Carmona Thursday, February 03, 2011 2:38 PM
- Unmarked As Answer by Ángel Manuel García Carmona Sunday, May 01, 2011 1:54 PM
- Marked As Answer by Ángel Manuel García Carmona Sunday, May 01, 2011 1:58 PM

