C# 删除控制台中以前写入的行
是否仍然可以使用console.SetCursorPosition()使用的(左、上)坐标删除console窗口的某些部分C# 删除控制台中以前写入的行,c#,console,C#,Console,是否仍然可以使用console.SetCursorPosition()使用的(左、上)坐标删除console窗口的某些部分 你能为它定制一个定制方法吗?Silky的评论是正确的答案: 设置适当的背景色 循环您希望清除的每一行的一部分: 将光标位置设置为左侧 写出一串宽度合适的空格 例如: public static void ClearArea(int top, int left, int height, int width) { ConsoleColor colorBefor
你能为它定制一个定制方法吗?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;
}
}
publicstaticvoidclearArea(int-top,int-left,int-height,int-width)
{
ConsoleColor colorBefore=Console.BackgroundColor;
尝试
{
Console.BackgroundColor=ConsoleColor.Black;
字符串空格=新字符串(“”,宽度);
对于(int i=0;i
请注意,这将恢复背景颜色,但不会恢复以前的光标位置。如果性能是一个问题,您可以直接操作控制台缓冲区。不幸的是,这将需要一些互操作,下面是一个例子,我已经改编自我给出的。这将很快清除控制台缓冲区的一个区域。由于互操作的原因,它有点长,但是如果您将它很好地包装到一个console helper类中,那么您可以扩展它来执行各种高性能的控制台输出
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;
}
}
}
使用系统;
使用System.IO;
使用System.Runtime.InteropServices;
使用Microsoft.Win32.SafeHandles;
命名空间控制台应用程序1
{
班级计划
{
[状态线程]
静态void Main(字符串[]参数)
{
Console.SetCursorPosition(0,0);
对于(整数x=0;x<80*25;++x)
{
控制台。写入(“A”);
}
Console.SetCursorPosition(0,0);
Console.ReadKey(true);
净空面积(1,1,78,23);
Console.ReadKey();
}
静态空隙净空面积(短左、短顶、短宽、短高)
{
ClearArea(左、上、宽、高、新CharInfo(){Char=new CharUnion(){ascichar=32}});
}
静态空隙净空面积(短左、短顶、短宽、短高、CharInfo charAttr)
{
使用(SafeFileHandle h=CreateFile(“CONOUT$”,0x40000000,2,IntPtr.Zero,FileMode.Open,0,IntPtr.Zero))
{
如果(!h.IsInvalid)
{
CharInfo[]buf=新的CharInfo[宽度*高度];
对于(int i=0;i
是和是。。。(说真的,你可以在你想“删除”的区域上写空格)。这会删除整行吗?因为我的程序是一个老式的星际游戏,我试图让飞船移动,所以我必须删除它的最后一个位置。@shorty876:不,它只会覆盖你指定的宽度。好的,我的想法行不通。我尝试了其他方法,但该继续了。你会得到一个清晰易懂的代码的答案