Print ASCII line art characters in C# console application

前端 未结 5 1984
清歌不尽
清歌不尽 2020-12-09 18:03

I would like to have a C# console application print the extended ASCII Codes from http://www.asciitable.com/. In particular I am looking at the line art characters: 169, 17

5条回答
  •  温柔的废话
    2020-12-09 18:07

    I don't know how to get ASCII to work but you can use Unicode, to some degree, if you want to. It does require that the console be set to a true type font.

    Michael Kaplan's article 'Anyone who says the console can't do Unicode isn't as smart as they think they are' includes the code for this.

    I couldn't get his code to work directly but this worked for me as long as I ran it from a True Type Font Console. The article includes how to set it.

    using System;
    using System.Runtime.InteropServices;
    namespace TestUnicode
    
    {
        class Program
    {
    
    
    
    public static void Main(string[] args) {
        string st = "\u0169\u0129\n\n";
        IntPtr stdout = GetStdHandle(STD_OUTPUT_HANDLE);
        uint written;
        WriteConsoleW(stdout, st, st.Length, out written, IntPtr.Zero);
    }
    
    
    [DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
    internal static extern bool WriteConsoleW(IntPtr hConsoleOutput,
                                              string lpBuffer,
                                              int nNumberOfCharsToWrite,
                                              out uint lpNumberOfCharsWritten,
                                              IntPtr lpReserved);
    
    
    internal static bool IsConsoleFontTrueType(IntPtr std) {
     CONSOLE_FONT_INFO_EX cfie = new CONSOLE_FONT_INFO_EX();
        cfie.cbSize = (uint)Marshal.SizeOf(cfie);
        if(GetCurrentConsoleFont(std, false, ref cfie)) {
            return(((cfie.FontFamily & TMPF_TRUETYPE) == TMPF_TRUETYPE));
        }
        return false;
    }
    
    
    
    [DllImport("Kernel32.DLL", ExactSpelling = true)]
    internal static extern IntPtr GetStdHandle(int nStdHandle);
    
    
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    internal static extern bool GetCurrentConsoleFont(IntPtr hConsoleOutput,
                                                        bool bMaximumWindow, 
                                                        ref CONSOLE_FONT_INFO_EX lpConsoleCurrentFontEx);
    
    
    
    internal struct COORD {
        internal short X;
        internal short Y;
        internal COORD(short x, short y) {
            X = x;
            Y = y;
        }
    }
    
    [StructLayout(LayoutKind.Sequential)]
    internal unsafe struct CONSOLE_FONT_INFO_EX {
        internal uint cbSize;
        internal uint nFont;
        internal COORD dwFontSize;
        internal int FontFamily;
        internal int FontWeight;
        fixed char FaceName[LF_FACESIZE];
    }
    
    internal const int TMPF_TRUETYPE = 0x4;
    internal const int LF_FACESIZE = 32;
    internal const string BOM = "\uFEFF";
    internal const int STD_OUTPUT_HANDLE = -11; // Handle to the standard output device.
    internal const int ERROR_INVALID_HANDLE = 6;
    internal const int ERROR_SUCCESS = 0;
    internal const uint FILE_TYPE_UNKNOWN = 0x0000;
    internal const uint FILE_TYPE_DISK = 0x0001;
    internal const uint FILE_TYPE_CHAR = 0x0002;
    internal const uint FILE_TYPE_PIPE = 0x0003;
    internal const uint FILE_TYPE_REMOTE = 0x8000;
    internal static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
    }
        }
    

提交回复
热议问题