How to change font size in console application using C
Although teppic's answer to use system()
will work, it is rather intensively heavy-handed to call an external program just to do that. As for David RF' answer, it is hard-coded for a specific type of terminal (probably a VT100-compatible terminal type) and won't support the user's actual terminal type.
In C, you should use terminfo capabilities directly:
#include <term.h>
/* One-time initialization near the beginning of your program */
setupterm(NULL, STDOUT_FILENO, NULL);
/* Enter bold mode */
putp(enter_bold_mode);
printf("I am bold\n");
/* Turn it off! */
putp(exit_attribute_mode);
Still, as teppic notes, there is no support for changing the font size. That's under the user's control.
Change Terminal Font Size with C++
At least for xterm
, you can change the current font by printing an escape sequence. The syntax is ESCAPE ] 50 ; FONTNAME BEL
.
Here's (an abbreviated version of) a script I use for this; I call it xfont
(the real one has more error checking):
#!/usr/bin/perl
use strict;
use warnings;
print "\e]50;@ARGV\a";
I don't know which other terminal emulators recognize this sequence. In particular, I find that it doesn't work under screen
, even if the screen
session is in an xterm
window.
Note that you have to specify the name of the font ("10x20"
, "9x15"
), not its size.
EDIT: I should pay more attention to tags. In C++, it would be something like:
std::cout << "\x1b]50;" << font_name << "\a" << std::flush;
UPDATE: With xterm
, this won't work if you're using TrueType fonts. Also, Dúthomhas suggests in a comment:
I know this is old, but all terminfo strings should be printed using
putp()
[ortputs()
], even in C++.
putp( (std::string{ "\33]50;" } + font_name + "\a").c_str() );
How to change the console font size
You can change the font size using SetCurrentConsoleFontEx
.
Below is a small example that you can play around with, make sure you #include <cwchar>
and #include <windows.h>
CONSOLE_FONT_INFOEX cfi;
cfi.cbSize = sizeof(cfi);
cfi.nFont = 0;
cfi.dwFontSize.X = 0; // Width of each character in the font
cfi.dwFontSize.Y = 24; // Height
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
std::wcscpy(cfi.FaceName, L"Consolas"); // Choose your font
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
std::cout << "Font: Consolas, Size: 24\n";
If you choose Arial or others, you may have to give it a font size width. For more information.
The difference between system()
calls and using Windows.h
is that system()
calls are resource heavy and unsafe. More information here.
C - How to change font size in Ncurses?
I do not believe it is possible. The terminal is not for such things. It is meant for displaying text in sometimes varying colors. If you want to change the font size, you would need to open a window and draw to it (That might not actually be the correct term. Graphics aren't my forte). If this is what you want to do, I suggest looking into sdl. It is fairly simple to set up and is easy (IMO) to use. And because I know stackoverflow doesn't like flamewars, I am by no means saying it is the best. Im sure there are plenty of alternatives that are just as good. I just have not used them
Changing text size
tl;dr: Not really, no.
Consoles are generally things that output plain text in some consistent way. Terminal emulators such as cmd.exe
, or PuTTY, or your Linux terminal, may well provide a way to change font name and size for the whole window. Furthermore, many POSIX-compliant terminals understand formatting systems like "ANSI Codes" that give a little control over colours and boldness, and I'm sure that Windows has similar functionality via WinAPI calls — these could be controlled by your C++ program. But none of this can take you out of the consistent-size, monospaced environment.
Option 1
Create a GUI instead. It seems like this is the direction you are headed, and it is the most appropriate solution if you really want fine, graphics-like control over how your program "looks".
Option 2
ASCII art:
_ _
| \ | | _
| \| | __ _ _ __ ___ ___(_)
| . ` |/ _` | '_ ` _ \ / _ \
| |\ | (_| | | | | | | __/_
|_| \_|\__,_|_| |_| |_|\___(_)
Jordan
Is it possible to get/set the console font size?
Maybe this article can help you
ConsoleHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
namespace ConsoleExtender {
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct ConsoleFont {
public uint Index;
public short SizeX, SizeY;
}
public static class ConsoleHelper {
[DllImport("kernel32")]
public static extern bool SetConsoleIcon(IntPtr hIcon);
public static bool SetConsoleIcon(Icon icon) {
return SetConsoleIcon(icon.Handle);
}
[DllImport("kernel32")]
private extern static bool SetConsoleFont(IntPtr hOutput, uint index);
private enum StdHandle {
OutputHandle = -11
}
[DllImport("kernel32")]
private static extern IntPtr GetStdHandle(StdHandle index);
public static bool SetConsoleFont(uint index) {
return SetConsoleFont(GetStdHandle(StdHandle.OutputHandle), index);
}
[DllImport("kernel32")]
private static extern bool GetConsoleFontInfo(IntPtr hOutput, [MarshalAs(UnmanagedType.Bool)]bool bMaximize,
uint count, [MarshalAs(UnmanagedType.LPArray), Out] ConsoleFont[] fonts);
[DllImport("kernel32")]
private static extern uint GetNumberOfConsoleFonts();
public static uint ConsoleFontsCount {
get {
return GetNumberOfConsoleFonts();
}
}
public static ConsoleFont[] ConsoleFonts {
get {
ConsoleFont[] fonts = new ConsoleFont[GetNumberOfConsoleFonts()];
if(fonts.Length > 0)
GetConsoleFontInfo(GetStdHandle(StdHandle.OutputHandle), false, (uint)fonts.Length, fonts);
return fonts;
}
}
}
}
Here is how to use it to list true type fonts for console,
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);
}
Crucial functions: SetConsoleFont
, GetConsoleFontInfo
and GetNumberOfConsoleFonts
. They're undocumented, so use at your own risk.
Related Topics
Gcc 7, -Wimplicit-Fallthrough Warnings, and Portable Way to Clear Them
How to Implement an Easy_Bind() That Automagically Inserts Implied Placeholders
Adding Header and .Cpp Files in a Project Built with Cmake
Scheduling Task in Using C++ on Linux and Windows Machine
What Is Double Evaluation and Why Should It Be Avoided
Could We Use Extern "C" in C File Without #Ifdef _Cplusplus
C++ Destruction of Temporary Object in an Expression
G++ Ld: Symbol(S) Not Found for Architecture X86_64
Cross-Platform C++: Use The Native String Encoding or Standardise Across Platforms
Xlib How Does This (Removing Window Decoration) Work
Why Do You Need to Append an L or F After a Value Assigned to a C++ Constant
How to Implement a Video Widget in Qt That Builds Upon Gstreamer
Why Are Std::Shuffle Methods Being Deprecated in C++14
Overloading Postfix and Prefix Operators
How to Make Camera Follow a 3D Object in Opengl