c# - 在 '/u263a' 上使用 WriteConsoleOutput 总是打印不正确

标签 c# unicode console

<分区>

看过this question并修改代码以打印“☺”、“☻”或“█”等字符,我看不出如何使用 WriteConsoleOutput 正确绘制这些字符。 (使用流或 Console.Write 时没有问题,但我无法使用这些解决方案单独控制字符颜色,因此它们不合适。)

似乎有一个解决方案here对于 C++,但他们在底部讨论的“L”宏不适用于 C#。

最后,查看了建议here ,我看不出我最终得到的代码有什么问题:

class Program
{
    [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    static extern SafeFileHandle CreateFile(
        string fileName,
        [MarshalAs(UnmanagedType.U4)] uint fileAccess,
        [MarshalAs(UnmanagedType.U4)] uint fileShare,
        IntPtr securityAttributes,
        [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
        [MarshalAs(UnmanagedType.U4)] int flags,
        IntPtr template
    );

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool WriteConsoleOutputW(
        SafeFileHandle hConsoleOutput,
        CharInfo[] lpBuffer,
        Coord dwBufferSize,
        Coord dwBufferCoord,
        ref SmallRect lpWriteRegion
    );

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool SetConsoleOutputCP(uint wCodePageID);

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool SetConsoleCP(uint wCodePageID);

    [StructLayout(LayoutKind.Sequential)]
    public struct Coord
    {
        public short X;
        public short Y;

        public Coord(short X, short Y)
        {
            this.X = X;
            this.Y = Y;
        }
    };

    [StructLayout(LayoutKind.Explicit)]
    public struct CharUnion
    {
        [FieldOffset(0)] public char UnicodeChar;
        [FieldOffset(0)] public byte AsciiChar;
    }

    [StructLayout(LayoutKind.Explicit)]
    public struct CharInfo
    {
        [FieldOffset(0)] public CharUnion Char;
        [FieldOffset(2)] public short Attributes;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SmallRect
    {
        public short Left;
        public short Top;
        public short Right;
        public short Bottom;
    }

    static void Main(string[] args)
    {
        //SetConsoleOutputCP(65001);
        //SetConsoleCP(65001);
        SafeFileHandle fileHandle = CreateFile("CONOUT$", 0x40000000, 2, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);
        Console.TreatControlCAsInput = true;
        //Console.OutputEncoding = Encoding.UTF8;
        Console.OutputEncoding = System.Text.Encoding.Unicode;
        Console.CursorVisible = false;
        short screenWidth = 40;
        short screenHeight = 20;
        Console.SetWindowSize(screenWidth + 1, screenHeight + 1);
        Console.SetBufferSize(screenWidth + 1, screenHeight + 1);
        bool running = true;
        while (running)
        {
            CharInfo[] buf = new CharInfo[screenWidth * screenHeight];
            SmallRect rect = new SmallRect() { Left = 0, Top = 0, Right = screenWidth, Bottom = screenHeight };
            for (int i = 0; i < buf.Length; ++i)
            {
                buf[i].Attributes = 6;
                buf[i].Char.UnicodeChar = '☺';
            }
            bool b = WriteConsoleOutputW(
                fileHandle,
                buf,
                new Coord() { X = screenWidth, Y = screenHeight },
                new Coord() { X = 0, Y = 0 },
                ref rect
            );
            Console.SetCursorPosition(0, 0);
        }
    }
}

我似乎已经排除了CharSetA/W DLLImport 后缀、code pagesConsole 属性和源文件 编码。

(当然,将彩色笑脸字符快速打印到 C# 控制台并非不可能。有人告诉我我在这里漏掉了一些愚蠢的东西;我花了大约 2 个小时谷歌搜索并尝试轻松模拟 1984 年管理的 Castle Adventure。)

我怎样才能让前面提到的 '☺' 打印除 '?' 以外的任何内容?还是“:”?

最佳答案

这似乎是 Using WriteConsoleOutput to write Unicode with c# 的副本

解决方案是将 CharSet = CharSet.Unicode 属性添加到 CharInfoCharUnion 结构。

关于c# - 在 '/u263a' 上使用 WriteConsoleOutput 总是打印不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62160502/

相关文章:

linux - 十六进制转义为文本(UTF-8)、bash 脚本

php - AJAX 请求返回 unicode 字符作为问号

c++ - 在 C 中打印 unicode 字符

Python 终端/文本 UI (TUI) 库

c# - Fluent NH 和接口(interface)映射

c# - 如何使用SelectTokens查询多个字段?

c# - 使用C#的电子邮件发送问题

c# - 与C#代码一起使用时,如何在Powershell中添加程序集引用?

java - 如何在Java中将控制台重定向到新窗口

python - Gnumeric Python 插件和控制台 - 较新的 Debian 安装中缺少功能