是否可以在控制台应用程序中的内容更改时在固定位置显示字符串?
Is it possible to display a string at a fixed position while content changes in a console app?
是否可以在 C# Windows 控制台应用程序中的固定位置显示变量的值,使其可见,否则屏幕内容会将每个旧值向上和向外推视线,不要在新行上一次又一次地打印值?
我知道 Curses,但想使用任何存在的标准,并学习如何使用它。
感谢您的宝贵时间和帮助。
Console.SetCursorPosition 是将插入符号定位在控制台特定位置所需的框架方法 windows。当然设置一个点然后从新点开始将所有后续写入控制台,所以,如果你想在精确位置写入一些东西然后从前一个点重新开始那么你还需要处理之前写入的位置.
这只是一个让您入门的示例
static void Main()
{
for (int x = 0; x < 100; x++)
{
if (x % 10 == 0)
Console.SetCursorPosition(0, 0);
else
Console.SetCursorPosition(0, x % 10);
Console.WriteLine(x);
WriteStatusText("Printing line " + x);
// Remove this comment to see it slowly
// Console.ReadLine();
}
Console.ReadLine();
}
static void WriteStatusText(string msg)
{
Console.SetCursorPosition(0, 10);
Console.WriteLine(msg);
}
请记住,将位置设置在为控制台定义的缓冲区(Console.BufferWidth和Console.BufferHeight)之外会触发异常
是否可以在 C# Windows 控制台应用程序中的固定位置显示变量的值,使其可见,否则屏幕内容会将每个旧值向上和向外推视线,不要在新行上一次又一次地打印值?
我知道 Curses,但想使用任何存在的标准,并学习如何使用它。
感谢您的宝贵时间和帮助。
Console.SetCursorPosition 是将插入符号定位在控制台特定位置所需的框架方法 windows。当然设置一个点然后从新点开始将所有后续写入控制台,所以,如果你想在精确位置写入一些东西然后从前一个点重新开始那么你还需要处理之前写入的位置.
这只是一个让您入门的示例
static void Main()
{
for (int x = 0; x < 100; x++)
{
if (x % 10 == 0)
Console.SetCursorPosition(0, 0);
else
Console.SetCursorPosition(0, x % 10);
Console.WriteLine(x);
WriteStatusText("Printing line " + x);
// Remove this comment to see it slowly
// Console.ReadLine();
}
Console.ReadLine();
}
static void WriteStatusText(string msg)
{
Console.SetCursorPosition(0, 10);
Console.WriteLine(msg);
}
请记住,将位置设置在为控制台定义的缓冲区(Console.BufferWidth和Console.BufferHeight)之外会触发异常