控制台状态栏
本文关键字:状态栏 控制台 | 更新日期: 2023-09-27 18:19:34
我有一个函数,可以用以下语句更新控制台应用程序中的状态栏:
Console.writeline("Status {0} Copied", i);
上面的语句的问题是,过一段时间后,它会在控制台行中填充语句,声明:
Status 1 Copied
Status 2 Copied
等等
有没有办法让它只刷新一行,这样1就会被2取代,以此类推,这样它就不会变成一堵文本墙?
要在当前行的开头重复写入输出,请尝试:
private static void SetStatusLineValue(int value)
{
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write("'rStatus {0} Copied", value);
}
甚至简单地说:Console.Write("'rStatus {0} Copied", value);
对于一个更灵活的解决方案,它要么"记住"起始行,要么使用指定行:
private static void SetTextForLine(string text, ref int line)
{
//set the status line for future reference
if (line < 0)
{
line = Console.CursorTop;
}
//save line/cursor state
int currentLine = Console.CursorTop;
bool cursorVisible = Console.CursorVisible;
Console.SetCursorPosition(0, line);
Console.WriteLine(text);
//restore state
Console.CursorTop = (currentLine == line ? currentLine + 1 : currentLine);
Console.CursorVisible = cursorVisible;
}
用法:
int statusLine = -1;
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Other lines in here...");
DoLongRunningOperation();
//update the status line here
SetTextForLine("Status " + i + " Copied", ref statusLine);
}
输出:
Other lines in here...
Status 9 Copied <- this line updating each SetTextForLine() call
Other lines in here...
Other lines in here...
Other lines in here...
Other lines in here...
Other lines in here...
Other lines in here...
Other lines in here...
Other lines in here...
Other lines in here...
是的,这是完全可能的,你只需要跟踪你所在的线路,然后覆盖它:
Console.Write("0000000000");
Console.SetCursorPosition(3, 0);
Console.Write("55");
Console.ReadLine();
产量:005500000
Console.WriteLine("0000000000");
Console.WriteLine("1111111111");
Console.WriteLine("2222222222");
Console.SetCursorPosition(3, 1);
Console.Write("55");
Console.ReadLine();
输出:
0000000000
1155111111
2222222222
您还可以使用方法Console.MoveBufferArea滚动控制台输出。
static void Main(string[] args)
{
Console.SetBufferSize(Console.WindowWidth, Console.WindowHeight);
Console.CursorLeft = 0;
Console.CursorTop = 0;
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
Console.Write("This is the statusbar".PadRight(Console.WindowWidth, ' '));
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.White;
int line = 1;
int totalLines = 1;
while (true) {
Thread.Sleep(100);
if (line == Console.WindowHeight){
// Scoll one line:
Console.MoveBufferArea(0, 2, Console.WindowWidth, Console.WindowHeight - 2, 0, 1);
line--;
}
Console.CursorLeft = 0;
Console.CursorTop = line;
Console.Write(string.Format("This is line {0}", totalLines));
totalLines++;
line++;
}
}
没有可能的方法,但您可以尝试一种解决方案,在编写下一条消息之前清除控制台屏幕。
Console.Clear();
另一个你可以尝试:"'r"
返回到当前行的开头并重写文本。
Console.Write("'rStatus {0} copied", i);
否则,您可以使用WinForms
我接受了这个答案,因为它帮助我生成了以下代码:
Console.WriteLine("Before Output");
for (int i = 0; i < 10; i++)
{
int currentLine = Console.CursorTop;
Console.WriteLine("i is {0}", i);
Thread.Sleep(500);
Console.SetCursorPosition(0, currentLine);
}
Console.WriteLine("After");
这是我将要使用的代码。