在控制台中隐藏滚动条而不会闪烁

And*_*ndy 3 c# scrollbar console-application

我在一秒钟内多次在控制台窗口中写入.

我想出了如何删除滚动条:

Console.BufferWidth = Console.WindowWidth = 35;
Console.BufferHeight = Console.WindowHeight;
Run Code Online (Sandbox Code Playgroud)

到目前为止一切都很好.但是当我想写一行的最后一列时,它会添加一个新行!这是合乎逻辑的,但如何避免这种情况?

我试图调整控制台的大小:

Console.BufferWidth++;
Console.BufferHeight++;

// Write to the last column of a line

Console.BufferWidth--;
Console.BufferHeight--;
Run Code Online (Sandbox Code Playgroud)

但这会闪烁,因为这些线会在一秒钟内多次执行!

任何想法或我将不得不与滚动条一起生活?

小智 8

试试Console.SetBufferSize(宽度,高度); 我认为这会有所帮助.


And*_*ndy 6

我使用本机方法进行绘图。

[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);

[StructLayout(LayoutKind.Sequential)]
public struct Coord
{
    public short X;
    public short Y;

    public Coord(short X, short Y)
    {
        this.X = X;
        this.Y = Y;
    }
};

[DllImport("kernel32.dll", SetLastError = true)]
    static extern bool WriteConsoleOutputCharacter(
    SafeFileHandle hConsoleOutput,
    string lpCharacter,
    int nLength,
    Coord dwWriteCoord,
    ref int lpumberOfCharsWritten);


public static void Draw(int x, int y, char renderingChar)
{
    // The handle to the output buffer of the console
    SafeFileHandle consoleHandle = CreateFile("CONOUT$", 0x40000000, 2, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero);

    // Draw with this native method because this method does NOT move the cursor.
    int n = 0;
    WriteConsoleOutputCharacter(consoleHandle, renderingChar.ToString(), 1, new Coord((short)x, (short)y), ref n);
}
Run Code Online (Sandbox Code Playgroud)

WriteConsoleOutputCharacter不移动光标。因此,即使在最后一行的最后一列中进行绘制,光标也不会跳到下一行(超出窗口大小)并破坏视图。