更新: 添加了一个示例
如果您准备做一些P / Invoke事情,这可能会有所帮助。
基本上,如果您获得了控制台缓冲区的句柄,则可以使用标准Win32 API来操纵缓冲区,甚至可以在屏幕之外构建整个缓冲区并将其绑定到控制台。
唯一棘手的部分是获取控制台缓冲区的句柄。我没有在.NET中尝试过此操作,但是在过去的几年中,您可以使用CreateFile获取当前控制台的句柄(您需要P /
Invoke this)并打开“ ConOUT $”,然后您可以使用返回传递给其他API。
P /为CreateFile调用
http://www.pinvoke.net/default.aspx/kernel32/CreateFile.html
您可以使用WriteConsoleOutput将所有字符及其属性从内存缓冲区移至控制台缓冲区。
http://msdn.microsoft.com/zh-
CN/library/ms687404(VS.85).aspx
您可能会组合一个不错的库来提供对控制台缓冲区的较低级别的访问。
由于我试图重新建立我的.NET,所以我想我会尝试一下,看看是否可以使它工作。这是一个示例,该示例将用所有字母AZ填充屏幕并贯穿所有forground属性0-15。我想您会对表演印象深刻。老实说,我没有花很多时间来审查此代码,因此错误检查为零,这里或那里可能有一个小错误,但它应该使您继续使用其余的API。
using System;using System.IO;using System.Runtime.InteropServices;using Microsoft.Win32.SafeHandles;namespace ConsoleApplication1{ class Program { [Dllimport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] 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 WriteConsoleOutput( SafeFileHandle hConsoleOutput, CharInfo[] lpBuffer, Coord dwBufferSize, Coord dwBufferCoord, ref SmallRect lpWriteRegion); [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 UnipreChar; [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; } [STAThread] static void Main(string[] args) { SafeFileHandle h = CreateFile("CONOUT$", 0x40000000, 2, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero); if (!h.IsInvalid) { CharInfo[] buf = new CharInfo[80 * 25]; SmallRect rect = new SmallRect() { Left = 0, Top = 0, Right = 80, Bottom = 25 }; for (byte character = 65; character < 65 + 26; ++character) { for (short attribute = 0; attribute < 15; ++attribute) { for (int i = 0; i < buf.Length; ++i) { buf[i].Attributes = attribute; buf[i].Char.AsciiChar = character; } bool b = WriteConsoleOutput(h, buf, new Coord() { X = 80, Y = 25 }, new Coord() { X = 0, Y = 0 }, ref rect); } } } Console.ReadKey(); } }}


