none
C# - Changing Console Font Programmatically RRS feed

  • Question

  • Hello, I have been trying to output a euro sign (€). However, when I do try to do this I get a ?, one way I have worked around this was by using the following code:

    Console.OutputEncoding = Encoding.Default;
    Console.WriteLine("€");
    And then changing the consoles font to be 'Lucida Console'. Is there any way to make the font be changing programmatically instead of having to do this manually.


    Sunday, May 24, 2015 7:42 AM

Answers

  • If you want to change the font programatically in Console app, there is no straight forward way for that unfortunatey, by using kernel api we can have this accomplished as per the below example:

     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            internal unsafe struct CONSOLE_FONT_INFO_EX
            {
                internal uint cbSize;
                internal uint nFont;
                internal COORD dwFontSize;
                internal int FontFamily;
                internal int FontWeight;
                internal fixed char FaceName[LF_FACESIZE];
            }
            [StructLayout(LayoutKind.Sequential)]
            internal struct COORD
            {
                internal short X;
                internal short Y;
    
                internal COORD(short x, short y)
                {
                    X = x;
                    Y = y;
                }
            }
            private const int STD_OUTPUT_HANDLE = -11;
            private const int TMPF_TRUETYPE = 4;
            private const int LF_FACESIZE = 32;
            private static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
    
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern bool SetCurrentConsoleFontEx(
                IntPtr consoleOutput,
                bool maximumWindow,
                ref CONSOLE_FONT_INFO_EX consoleCurrentFontEx);
    
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern IntPtr GetStdHandle(int dwType);
    
    
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern int SetConsoleFont(
                IntPtr hOut,
                uint dwFontNum
                );
            static void Main(string[] args)
            {
                SetConsoleFont();
                int value = 977788899;
                Console.OutputEncoding = Encoding.UTF8;
                Console.WriteLine("Price: " + value.ToString("C", CultureInfo.CreateSpecificCulture("fr-FR")));
    
    
                Console.ReadLine();
            }
            public static void SetConsoleFont(string fontName = "Lucida Console")
            {
                unsafe
                {
                    IntPtr hnd = GetStdHandle(STD_OUTPUT_HANDLE);
                    if (hnd != INVALID_HANDLE_VALUE)
                    {
                        CONSOLE_FONT_INFO_EX info = new CONSOLE_FONT_INFO_EX();
                        info.cbSize = (uint)Marshal.SizeOf(info);
    
                        // Set console font to Lucida Console.
                        CONSOLE_FONT_INFO_EX newInfo = new CONSOLE_FONT_INFO_EX();
                        newInfo.cbSize = (uint)Marshal.SizeOf(newInfo);
                        newInfo.FontFamily = TMPF_TRUETYPE;
                        IntPtr ptr = new IntPtr(newInfo.FaceName);
                        Marshal.Copy(fontName.ToCharArray(), 0, ptr, fontName.Length);
    
                        // Get some settings from current font.
                        newInfo.dwFontSize = new COORD(info.dwFontSize.X, info.dwFontSize.Y);
                        newInfo.FontWeight = info.FontWeight;
                        SetCurrentConsoleFontEx(hnd, false, ref newInfo);
                    }
                }
            }

    You have to enable the allow unsafe code in the Project Properties>Build>Allow Unsafe Code.

    And the result:

    Reference:

    http://stackoverflow.com/questions/20631634/changing-font-in-a-console-window-in-c-sharp


    Fouad Roumieh


    Sunday, May 24, 2015 9:23 AM

All replies

  • If you want to change the font programatically in Console app, there is no straight forward way for that unfortunatey, by using kernel api we can have this accomplished as per the below example:

     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            internal unsafe struct CONSOLE_FONT_INFO_EX
            {
                internal uint cbSize;
                internal uint nFont;
                internal COORD dwFontSize;
                internal int FontFamily;
                internal int FontWeight;
                internal fixed char FaceName[LF_FACESIZE];
            }
            [StructLayout(LayoutKind.Sequential)]
            internal struct COORD
            {
                internal short X;
                internal short Y;
    
                internal COORD(short x, short y)
                {
                    X = x;
                    Y = y;
                }
            }
            private const int STD_OUTPUT_HANDLE = -11;
            private const int TMPF_TRUETYPE = 4;
            private const int LF_FACESIZE = 32;
            private static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
    
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern bool SetCurrentConsoleFontEx(
                IntPtr consoleOutput,
                bool maximumWindow,
                ref CONSOLE_FONT_INFO_EX consoleCurrentFontEx);
    
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern IntPtr GetStdHandle(int dwType);
    
    
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern int SetConsoleFont(
                IntPtr hOut,
                uint dwFontNum
                );
            static void Main(string[] args)
            {
                SetConsoleFont();
                int value = 977788899;
                Console.OutputEncoding = Encoding.UTF8;
                Console.WriteLine("Price: " + value.ToString("C", CultureInfo.CreateSpecificCulture("fr-FR")));
    
    
                Console.ReadLine();
            }
            public static void SetConsoleFont(string fontName = "Lucida Console")
            {
                unsafe
                {
                    IntPtr hnd = GetStdHandle(STD_OUTPUT_HANDLE);
                    if (hnd != INVALID_HANDLE_VALUE)
                    {
                        CONSOLE_FONT_INFO_EX info = new CONSOLE_FONT_INFO_EX();
                        info.cbSize = (uint)Marshal.SizeOf(info);
    
                        // Set console font to Lucida Console.
                        CONSOLE_FONT_INFO_EX newInfo = new CONSOLE_FONT_INFO_EX();
                        newInfo.cbSize = (uint)Marshal.SizeOf(newInfo);
                        newInfo.FontFamily = TMPF_TRUETYPE;
                        IntPtr ptr = new IntPtr(newInfo.FaceName);
                        Marshal.Copy(fontName.ToCharArray(), 0, ptr, fontName.Length);
    
                        // Get some settings from current font.
                        newInfo.dwFontSize = new COORD(info.dwFontSize.X, info.dwFontSize.Y);
                        newInfo.FontWeight = info.FontWeight;
                        SetCurrentConsoleFontEx(hnd, false, ref newInfo);
                    }
                }
            }

    You have to enable the allow unsafe code in the Project Properties>Build>Allow Unsafe Code.

    And the result:

    Reference:

    http://stackoverflow.com/questions/20631634/changing-font-in-a-console-window-in-c-sharp


    Fouad Roumieh


    Sunday, May 24, 2015 9:23 AM
  • Can I use the code above and modify it so I can change the size of the font and if so how?
    Monday, June 8, 2015 12:52 PM