Console.SetWindowSize()与Console.Clear()未按预期工作及窗口乱码问题
控制台窗口尺寸设置与输出异常问题
我正在开发一个简单的控制台应用,想要把控制台窗口设置为特定尺寸。试过用Console.SetWindowSize()和Console.WindowHeight/WindowWidth,但实际窗口大小根本没变化——这些方法只修改了窗口内可绘制文本的区域,和预期完全不符。
另外Console.Clear()的表现也很诡异:有时候能清掉之前的输出,有时候不行,新输出直接覆盖旧内容,旧内容偶尔还能看见。手动拖动调整窗口大小后,输出更是乱套,新旧数据好像随机互相覆盖。
代码示例
public TestScreen() { Console.WindowHeight = 20; Console.WindowWidth = 20; Console.WriteLine("Test 1"); Console.WriteLine("Max height: " + Console.LargestWindowHeight.ToString()); Console.WriteLine("Max width: " + Console.LargestWindowWidth.ToString()); Console.ReadKey(); Console.Clear(); Console.SetWindowSize(10, 10); Console.WriteLine("Test 2"); Console.WriteLine("Max height: " + Console.LargestWindowHeight.ToString()); Console.WriteLine("Max width: " + Console.LargestWindowWidth.ToString()); Console.ReadKey(); Console.Clear(); }
结果截图说明
- 截图1:初始运行状态
- 截图2:按下任意键后的状态
- 截图3:手动调整窗口大小后的状态
内容的提问来源于stack exchange,提问作者SkeptiPunk
相关产品推荐
相关产品推荐

