在Console中删除以前写入的行

时间:2010-07-04 04:48:36

标签: c# console

是否有使用Console.SetCursorPosition()使用的(左,上)坐标删除控制台窗口的某些部分?

你能为它制作一个自定义方法吗?

2 个答案:

答案 0 :(得分:4)

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;
    }
}

请注意,这将恢复背景颜色,但上一个光标位置。

答案 1 :(得分:4)

如果需要考虑性能,可以直接操作控制台缓冲区。不幸的是,这需要一些互操作,这是我改编自我给出的previous answer的一个例子。这将很快清除控制台缓冲区的一个区域。由于interop,它有点冗长,但是如果你将它很好地包装到一个控制台帮助器类中,你可以扩展它以执行各种高性能控制台输出。

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;
    }   
  }
}