在Console中删除以前写入的行
无论如何使用Console.SetCursorPosition()使用的(Left,Top)坐标删除控制台窗口的某些部分?
你能为它制作一个自定义方法吗?
Silky的评论是正确的答案:
例如:
public static void ClearArea(int top, int left, int height, int width) { ConsoleColor colorBefore = Console.BackgroundColor; try { Console.BackgroundColor = ConsoleColor.Black; string spaces = new string(' ', width); for (int i = 0; i < height; i++) { Console.SetCursorPosition(left, top + i); Console.Write(spaces); } } finally { Console.BackgroundColor = colorBefore; } }
请注意,这将恢复背景颜色,但不恢复上一个光标位置。
如果性能受到关注,您可以直接操作控制台缓冲区。 不幸的是,这将需要一些互操作,这是一个我从先前的答案中改编的例子。 这将很快清除控制台缓冲区的一个区域。 由于互操作性,它有点冗长,但如果将它很好地包装到控制台助手类中,则可以将其扩展为执行各种高性能控制台输出。
上述就是C#学习教程:在Console中删除以前写入的行分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注---计算机技术网(www.ctvol.com)!
using System; using System.IO; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; namespace ConsoleApplication1 { class Program { [STAThread] static void Main(string[] args) { Console.SetCursorPosition(0, 0); for (int x = 0; x < 80 * 25; ++x) { Console.Write("A"); } Console.SetCursorPosition(0, 0); Console.ReadKey(true); ClearArea(1, 1, 78, 23); Console.ReadKey(); } static void ClearArea(short left, short top, short width, short height) { ClearArea(left, top, width, height, new CharInfo() { Char = new CharUnion() { AsciiChar = 32 } }); } static void ClearArea(short left, short top, short width, short height, CharInfo charAttr) { using (SafeFileHandle h = CreateFile("CONOUT$", 0x40000000, 2, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero)) { if (!h.IsInvalid) { CharInfo[] buf = new CharInfo[width * height]; for (int i = 0; i < buf.Length; ++i) { buf[i] = charAttr; } SmallRect rect = new SmallRect() { Left = left, Top = top, Right = (short)(left + width), Bottom = (short)(top + height) }; WriteConsoleOutput(h, buf, new Coord() { X = width, Y = height }, new Coord() { X = 0, Y = 0 }, ref rect); } } } [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)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool CloseHandle(IntPtr hObject); [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 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; } } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/951210.html