如果您想用您的字符填充整个控制台窗口,一种可能的方法是将光标移回 0,0 位置。
例子:
Console.CursorVisible = false;
for(int i = 0; i < Console.WindowHeight * Console.WindowWidth; i ++)
{
Console.Write((i / Console.WindowWidth) % 10); // print your stuff
}
Console.SetCursorPosition(0, 0);
Console.ReadKey();
所以你可以在你的方法中这样做:
private void DumpCharacters()
{
for (int y = 0; y < yMax; y++)
{
string line = string.Empty;
for (int x = 0; x < xMax; x++)
{
line += characters[x, y];
}
Console.SetCursorPosition(0, y);
Console.Write(line);
}
Console.SetCursorPosition(0, 0);
}
请注意,您不必从 yMax 中减去 1。这是因为现在您也可以使用控制台屏幕的最后一行。
这是生成所需结果的完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleChar
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Stackoverflow - Super example";
Console.CursorVisible = false;
int yMax = Console.WindowHeight;
int xMax = Console.WindowWidth;
char[,] characters= new char[Console.WindowWidth, Console.WindowHeight];
for (int i = 0; i < Console.WindowWidth; i++ )
{
for (int j = 0; j < Console.WindowHeight; j++)
{
char currentChar = ' ';
if((i == 0) || (i == Console.WindowWidth - 1))
{
currentChar = '║';
}
else
{
if((j == 0) || (j == Console.WindowHeight - 1))
{
currentChar = '═';
}
}
characters[i, j] = currentChar;
}
}
characters[0, 0] = '╔';
characters[Console.WindowWidth-1, 0] = '╗';
characters[0, Console.WindowHeight - 1] = '╚';
characters[Console.WindowWidth - 1, Console.WindowHeight - 1] = '╝';
for (int y = 0; y < yMax ; y++)
{
string line = string.Empty;
for (int x = 0; x < xMax; x++)
{
line += characters[x, y];
}
Console.SetCursorPosition(0, y);
Console.Write(line);
}
Console.SetCursorPosition(0, 0);
}
}
}