为什么将字符写在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();
}
}
结果输出: