c# – How to change the font of the program in C # console?

Question:

The following C # code is successfully compiled into an executable file and subsequently run:

using System;

namespace fuenteLetra {
    class Programa {
        public static void Main(string[] args){
            //PARÁMETROS
            Console.Title = "Título de la ventana";

            //PROGRAMA
            Console.Write("El programa tiene otra fuente de letra");
        }
    }
}

However, the current console is Windows 7, so the font is bitmap font. In Windows 10, the font changes to Consolas and / or Lucida Console.

I have investigated but very complex functions appear to change the font.

Can you change the font of a console program with a simple parameter or similar?

I guess it may be something like Console.Font = "Consolas" , but it doesn't work.

Answer:

As you have seen, it is not trivial because you are going to modify the behavior of an external application, which is also not very versatile and tends to be half coupled to the OS.

Here they comment on an option, where you create a Helper with some methods that help you to do that task and then you invoke it in your program using the SetCurrentFont method that implements the Helper.

They propose a helper ( ConsoleHelper.cs ) like this:

using System;
using System.Runtime.InteropServices;

public static class ConsoleHelper
{
    private const int FixedWidthTrueType = 54;
    private const int StandardOutputHandle = -11;

    [DllImport("kernel32.dll", SetLastError = true)]
    internal static extern IntPtr GetStdHandle(int nStdHandle);

    [return: MarshalAs(UnmanagedType.Bool)]
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    internal static extern bool SetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool MaximumWindow, ref FontInfo ConsoleCurrentFontEx);

    [return: MarshalAs(UnmanagedType.Bool)]
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    internal static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool MaximumWindow, ref FontInfo ConsoleCurrentFontEx);


    private static readonly IntPtr ConsoleOutputHandle = GetStdHandle(StandardOutputHandle);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct FontInfo
    {
        internal int cbSize;
        internal int FontIndex;
        internal short FontWidth;
        public short FontSize;
        public int FontFamily;
        public int FontWeight;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
        //[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.wc, SizeConst = 32)]
        public string FontName;
    }

    public static FontInfo[] SetCurrentFont(string font, short fontSize = 0)
    {
        Console.WriteLine("Set Current Font: " + font);

        FontInfo before = new FontInfo
        {
            cbSize = Marshal.SizeOf<FontInfo>()
        };

        if (GetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref before))
        {

            FontInfo set = new FontInfo
            {
                cbSize = Marshal.SizeOf<FontInfo>(),
                FontIndex = 0,
                FontFamily = FixedWidthTrueType,
                FontName = font,
                FontWeight = 400,
                FontSize = fontSize > 0 ? fontSize : before.FontSize
            };

            // Get some settings from current font.
            if (!SetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref set))
            {
                var ex = Marshal.GetLastWin32Error();
                Console.WriteLine("Set error " + ex);
                throw new System.ComponentModel.Win32Exception(ex);
            }

            FontInfo after = new FontInfo
            {
                cbSize = Marshal.SizeOf<FontInfo>()
            };
            GetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref after);

            return new[] { before, set, after };
        }
        else
        {
            var er = Marshal.GetLastWin32Error();
            Console.WriteLine("Get error " + er);
            throw new System.ComponentModel.Win32Exception(er);
        }
    }
}

Notice how it loads the dlls at the beginning so that you can interact with the console configuration.

To use the helper, in your code you would do something like

ConsoleHelper.SetCurrentFont("Consolas", 10);

Or you can go for the non-programmatic option, go to the Console profile settings for your Windows session, change the font and that's it 🙂

Scroll to Top