Home > database >  A way of writing a line in a specific font [C# Console]
A way of writing a line in a specific font [C# Console]

Time:09-11

Alright I basically want to write a specific line in a different font, example:

Console.WriteLine("Line with regular font");
Console.WriteLine("Line with a special font");
Console.WriteLine("Line with regular font");
Console.WriteLine("Line with regular font");

Hope you get what I'm saying, and of course on a console app.

I already know this method: Console.OutputEncoding =

But that would change the whole console font, and I need a specific line

CodePudding user response:

Console.OutputEncoding has nothing to do with fonts. It's encoding, like ASCII or UTF-8. You cannot do that because a console doesn't deal with fonts. It's just a stream of characters.

If you want to be change the font on a line by line basis you need to make your own GUI application to display the lines of text in the window.

CodePudding user response:

Only by using kernel api, don't forget to enable unsafe code in Project Properties>Build>Allow Unsafe Code

 [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);

                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);

                newInfo.dwFontSize = new COORD(info.dwFontSize.X, info.dwFontSize.Y);
                newInfo.FontWeight = info.FontWeight;
                SetCurrentConsoleFontEx(hnd, false, ref newInfo);
            }
        }
    }
  • Related