WriteConsole输出错误的间距

时间:2019-01-01 01:57:15

标签: c# console console-application windows-console

为什么将字符写在1个单元格中? Windows控制台API,但是现在我遇到了一个让我伤脑筋的问题。应该只显示一条线,但是要显示点

我的代码:

[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_SCREEN_BUFFER_INFO
{
    public COORD dwSize;
    public COORD dwCursorPosition;
    public short wAttributes;
    public SmallRect srWindow;
    public COORD dwMaximumWindowSize;
}
[StructLayout(LayoutKind.Explicit)]
public struct CharInfo
{
    [FieldOffset(0)]
    internal char UnicodeChar;
    [FieldOffset(0)]
    internal short AsciiChar;
    [FieldOffset(2)]
    internal COLOR Attributes;
}
[StructLayout(LayoutKind.Sequential)]
public struct SmallRect
{
    public short Left;
    public short Top;
    public short Right;
    public short Bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct COORD
{
    public short X;
    public short Y;
    public COORD(short X, short Y)
    {
        this.X = X;
        this.Y = Y;
    }
}
public class Engine
{
    public short Width { get; protected set; }
    public short Height { get; protected set; }
    public CharInfo[] buffer;
    protected SmallRect rect;
    protected const int STDIN = -10;
    protected const int STDOUT = -11;
    protected const int STDERR = -12;
    public Engine(short width = 80, short height = 25)
    {
        Width = width;
        Height = height;
        buffer = new CharInfo[width * height];
        rect  = new SmallRect() { Left = 0, Top = 0, Right = width, Bottom = height };
        SetConsoleCP(437); SetConsoleOutputCP(437);
        Console.SetBufferSize(width, height);
        Console.SetWindowSize(width, height);
    }
    public void SetPixel(short x, short y, Character symbol, COLOR color)
    {
        SetPixel(x, y, new CharInfo() {Attributes = color, UnicodeChar = (char)symbol });
    }
    protected void SetPixel(short x, short y, CharInfo @char)
    {
        if (x >= 0 && y >= 0 && x < Width && y < Height)
        {
            buffer[y * Width + x] = @char;
        }
        WriteConsoleOutput(GetStdHandle(STDOUT), buffer, new COORD(80, 25), new COORD(0, 0), ref rect);
    }

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

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

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool WriteConsoleOutput(IntPtr hConsoleOutput, CharInfo[] buffer, COORD bufferSize, COORD bufferCoord, ref SmallRect region);

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

    [DllImport("kernel32.dll")]
    static extern uint GetLastError();

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern bool GetConsoleScreenBufferInfo(IntPtr hConsoleOutput, out CONSOLE_SCREEN_BUFFER_INFO buffer);

}


 class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        Engine e = new Engine();
        for (short i = 0; i < 10; i++)
        {
            e.SetPixel(i, 1, Character.Full, COLOR.FG_RED);
        }
        Console.ReadLine();
    }
}

结果输出:

result

1 个答案:

答案 0 :(得分:0)

尽管您没有显示它,但我认为错误的是您对颜色的定义。颜色可能是枚举类型。确保此类型的基类型为short,而不是int。

enum COLOR : short
{
    FW_RED = 0x0004,
}

如果省略“:short”,则编译器将生成一个CharInfo结构,其大小为6个字节,而不是4个字节。

使用正确的定义,我得到一整行:

full line