谁能解释一下为什么在控制台的底部和右侧有一个区域当我运行这段代码时,我不能选择该区域中的任何东西,代码会生成一个没有角的矩形,这是我看到的图像:

编辑:这个区域似乎只对我显示,即使我尝试他们的代码,它也不会显示在其他人的图像上。非常奇怪。
编辑2:以红色突出显示的区域:

class Program
{
static void Main(string[] args)
{
int w = 50;
int h = 10;
Console.SetWindowSize(w, h);
Console.SetBufferSize(w, h);
Console.SetCursorPosition(0, 0);
Console.Write(" " + new string('#', w-2));
for(int i = 0; i < h - 2; i++)
{
Console.SetCursorPosition(0, i + 1);
Console.Write("#" + new string(' ', w-2) + "#");
}
Console.SetCursorPosition(0, h-1);
Console.Write(" " + new string('#', w - 2));
Console.ReadKey();
}
}发布于 2020-07-11 14:00:37
您可以使窗口大小和缓冲区大小1个字符大于您的参考区域。这允许在不触发滚动条的情况下写入填充区域的所有字符。
您忽略了在位置(w, h)中写入最后一个字符,否则控制台将滚动。然后使用Console.MoveBufferArea()将缓冲区中的字符复制到最后一个位置。此方法仅使用缓冲区,它实际上并不写入控制台,因此控制台窗口不会滚动。
替换函数移动的字符--这次写到控制台--这个区域就完全填满了。
如果控制台显示与buffer-无关的缓冲区大小:如果您以前没有执行过此操作,请从控制台菜单中打开控制台属性,然后在Discard old duplicates面板的Command History部分中选择Options。
您还可以在注册表中将HKEY_CURRENT_USER\Console\HistoryNoDup值设置为1。我通常在控制台应用程序启动时执行此操作,并在其关闭时重新设置先前的值(您只需设置它一次-它位于当前用户分支中,不需要管理员权限)。

static void Main(string[] args)
{
DrawConsoleArea(50, 10, '#', false);
}
internal static void DrawConsoleArea(int width, int height, char chr, bool showCursor)
{
Console.CursorVisible = showCursor;
Console.SetWindowSize(width + 1, height + 1);
Console.SetBufferSize(width + 1, height + 1);
for (int i = 1; i < height; i++) {
Console.SetCursorPosition(0, i);
Console.Write(chr + new string(' ', width - 1) + chr);
}
Console.SetCursorPosition(0, 0);
Console.Write(new string(chr, width + 1));
Console.SetCursorPosition(0, height);
Console.Write(new string(chr, width));
Console.MoveBufferArea(0, 0, 1, 1, width, height);
Console.SetCursorPosition(0, 0);
Console.Write(chr);
}发布于 2020-07-11 03:42:41
您在Console.ReadKey();中看到的光标
如果您可以将窗口高度增加1行,然后将光标推到底部。
Console.SetWindowSize(w, h+1);
Console.SetBufferSize(w, h+1);
//...
Console.WriteLine(" " + new string('#', w - 2));
Console.Write("Press any key to exit.");
Console.ReadKey();或者,将光标可见性隐藏为false。此外,您可能希望执行ReadKey(true),否则键盘上按下的字符将被写在角落中。
但是,确保在之后使其可见,以防您需要它在任何用户交互中可见。
Console.CursorVisible = false;
Console.ReadKey(true);
//...
Console.CursorVisible = true;用图片编辑:用你的代码:

使用Console.CursorVisible = false;

https://stackoverflow.com/questions/62841038
复制相似问题