填充控制台中的最后一行 [英] Filling last line in console

查看:58
本文介绍了填充控制台中的最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想填充/更新控制台的整个底线。示例:

I would like to fill/update the entire bottom line of the console. Example:

static void Main(string[] args)
{
    Console.BufferWidth = Console.WindowWidth;
    Console.BufferHeight = Console.WindowHeight;
    Console.CursorTop = Console.WindowHeight - 1;
    string s = "";
    for(int i = 0; i < Console.BufferWidth; i++)
        s += (i%10).ToString();
    Console.Write(s);
    Console.CursorTop = 0;
    Console.ReadKey();
}

问题是,当文本被打印时,它会移动到新行。提出了类似的问题来将光标移动到0,0,但是仅在缓冲区大小大于窗口大小时才有效,并且我希望缓冲区宽度和窗口宽度相等(以删除滚动条)。
有什么想法吗?我能得到的最接近的是打印到较高的行并将其移动到最后一行,但是在整个项目中这是不可接受的。

The issue is that when the text is printed, it moves to a new line. Similar questions stated to move the cursor to 0,0, however that only works when the buffer size is larger than the window size, and I would like the buffer width and the window width to be equal (to remove scroll bars). Any ideas? The closest I could get is printing to a higher line and moving it to the last line, however that will not be acceptable in the full project.

编辑:
该问题的最后一句话专门讨论了移动缓冲区区域。可以通过以下示例看到无法正常运行的原因:

The last sentence of the question was specifically talking about movebufferarea. The reason why that won't work can be seen by this example:

static void Main(string[] args)
{
    Console.BufferWidth = Console.WindowWidth;
    Console.BufferHeight = Console.WindowHeight;
    while (!Console.KeyAvailable)
    {
        Console.CursorTop = Console.WindowHeight - 2;
        string s = "";
        for (int i = 0; i < Console.BufferWidth; i++)
            s += (i % 10).ToString();
        Console.Write(s);
        Console.MoveBufferArea(0, Console.WindowHeight - 2, Console.WindowWidth, 1, 0, Console.WindowHeight - 1);
        Thread.Sleep(10);
    }
}

由于打印出来的句子经常闪烁首先,然后移动。

The sentence will flicker every so often because it is printed first and then moved.

推荐答案

由于光标始终位于您编写的文本之后,因此您可以少写一个字符为避免转到下一行,或直接将字符直接写入缓冲区(我相信,控制台。MoveBufferArea可以用于此)。

Since the cursor is always trailing after the text you've written you can either write one less character to avoid going to the next line, or simply write the characters directly into the buffer (I believe, Console.MoveBufferArea can be used for that).

这篇关于填充控制台中的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆