I'm trying to change the size of the console and its buffer to 80/25. Here's the code that I tried:
//ConsoleUtils u = new ConsoleUtils(); <- dummy class
while (true) {
Console.SetBufferSize(Console.WindowLeft + 80, Console.WindowTop + 25);
Console.SetWindowSize(80, 25);
System.Threading.Thread.Sleep(1); //To minimize processor ussage %.
}
Everything looks okay but after I try to resize the console, a wild error about ArgumentOutOfBounds appears (depending on the way I scale the console). How do I make the console size permanent? E.g. I want it to be 80x25 and disallow the user from resizing it.
Your code has one error which may or may not influence results:
Console.SetBufferSize(Console.WindowLeft + 80, Console.WindowTop + 25);
sets buffer size depending on console window top left coordinates.
Console.WindowLeft
and Console.WindowTop
properties allow to get or set top left position of Console window.
Second error is a lack for check of Console Window and Buffer sizes before size change is made to adjust order of operations to requirement of Window size being always smaller than Buffer size. The order in which resizing of Console Buffer and Console Window are done depends on relation between target size and original size. To understand it better it is necessary to understand relation between Console Window size and Console Buffer size. Console Window is a sliding window which allows to display content of Console Buffer. Therefore if Console Window height or width is larger than Buffer height or width ArgumentOutOfRangeException is thrown (in .NET there is no ArgumentOutOfBounds exception type).
To avoid that an order in which size of Buffer and size of Window changes does matter and it has to be taken care of in every dimension. To better understand problem I have created sample program which will just do what you want and allow for better understanding of Console size change mechanisms - it is not production code !!!. In practice it would be much simpler to do this using Console.SetBufferSize and Console.SetWindowSize methods.
using System;
using System.Threading;
using static System.Console;
namespace ResizeConsoleWindow
{
class Program
{
static void Main(string[] args)
{
int targetWindowWidth = 80;
int targetWindowHeight = 25;
Timer timer = new Timer(
(object state) =>
{
int originalWindowWidth = WindowWidth;
int originalWindowHeight = WindowHeight;
int originalBufferWidth = BufferWidth;
int originalBufferHeight = BufferHeight;
if (targetWindowWidth <= originalWindowWidth)
{
WindowWidth = targetWindowWidth;
BufferWidth = targetWindowWidth;
if (targetWindowHeight <= originalWindowHeight)
{
WindowHeight = targetWindowHeight;
BufferHeight = targetWindowHeight;
}
else
{
BufferHeight = targetWindowHeight;
WindowWidth = targetWindowWidth;
WindowHeight = targetWindowHeight;
}
}
else
{
BufferWidth = targetWindowWidth;
WindowWidth = targetWindowWidth;
if (targetWindowHeight <= originalWindowHeight)
{
WindowHeight = targetWindowHeight;
BufferHeight = targetWindowHeight;
}
else
{
BufferHeight = targetWindowHeight;
WindowHeight = targetWindowHeight;
}
}
WindowWidth = targetWindowWidth;
WindowHeight = targetWindowHeight;
}, state: null, dueTime: 0, period: 200);
while (true)
{
WriteLine("Press Enter to exit ... ");
ReadLine();
break;
}
}
}
}
To play around start program and try to change Console Window size to smaller than target or larger than target and see what happens (to make it easier to observe increase tick duration in timer) or even debug program while changing Console Window size and set breakpoints to check how the change is done. Than change order and check if exception is thrown and when.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With