The Console class also recalls some of my Unix programming memories.
When I was a Unix programmer, I used a terminal called SecureCRT, whose window width can be wider than 80 characters.
Is 80 enough? Generally it is. But when I try to illustrate the data on the screen or draw ASCII art. I'll feel the limitation.
The Console class in C# is easy to tune. Much more convenient than in Unix.
When you want to set the width, just use:
Console.WindowWidth = XXX;
One thing you should pay attention on:
The WindowWidth has a limit, it cannot be wider than your screen.
For example, if your screen resoluation is 1024*768, then the max width is 115. While 1280*1024, max width is 147.
We can get the limitation by property Console.LargestWindowWidth. So we can maximise our console window by:
Console.WindowWidth = Console.LargestWindowWidth;
Console.WindowHeight = Console.LargestWindowHeight;
Some other useful method and properties
BufferHeight
When you wanna keep more lines of output on the screen, you can modify this value(300 by default)
Beep(+1 overload)
When you running a time-consuming job, just leave it running there, and let Beep notify you when the job is completed.
You can also play a beep song by its overload.
Clear()
When we run a application which should keep some tracks, just like a path searching application. Only one path, the best one, need to be kept on the screen. Don't bother to judge whether the path should be printed, just print them on the console. The only thing we need to do is clear it when it isn't the best path.
MoveBufferArea(srcLeft, srcTop, width, height, dstLeft, dstTop)
This method is really cool, but first we should make clear that it is MoveBufferArea, not CopyBufferArea.
Assume we have a method to print a buffer in binary format. Now we wanna compare two buffers, but don't wanna modify the method, so...
Look at the sample code and the output screen snap:
using System.Text;
using System.Runtime.InteropServices;
namespace LearnCSharp
{
class TestApplication
{
/// <summary>
/// Tell the bit is on or off
/// </summary>
/// <param name="buf">The buffer which holds the bits</param>
/// <param name="byteOffset">The byte offset in the buffer</param>
/// <param name="bitOffset">The bit offset in a byte, following little endian bit order rule</param>
/// <returns>return 1 If the specific bit is on, return 0 if the bit is off</returns>
private static int BigEndianPeekBit(byte[] buf, int byteOffset, int bitOffset)
{
byte mask = (byte)(0x80 >> (bitOffset % 8));
return (buf[byteOffset + bitOffset / 8] & mask) != 0 ? 1 : 0;
}
/// <summary>
/// Display the specified bytes in binary format
/// </summary>
/// <param name="buf">The buffer which holds the bits</param>
/// <param name="byteOffset">The byte offset in the buffer</param>
/// <param name="bytesCount">The number of bytes to display</param>
/// <returns>A string in binary formatt for diagnosing purpose</returns>
public static string ToBinaryFormat(byte[] buf, int byteOffset, int bytesCount)
{
if (byteOffset < 0 || bytesCount <= 0)
{
throw new Exception("invalid parameter : byteOffset < 0 or bytesCount <= 0");
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytesCount; i++)
{
sb.AppendFormat("{0:d4}: ", i);
for (int j = 0; j < 8; j++)
{
// code hack: big endian bit order is the reverse of little endian bit order
if (BigEndianPeekBit(buf, byteOffset + i, j) == 1)
{
sb.Append('1');
}
else
{
sb.Append('0');
}
}
sb.Append('\n');
}
return sb.ToString();
}
public static void Main()
{
byte[] array = new byte[16];
for (int i = 0; i < array.Length; i++)
{
array[i] = (byte)i;
}
// The area we want
int srcLeft = Console.CursorLeft + 5;
int srcTop = Console.CursorTop;
int width = 9;
int height = 16;
Console.Write(ToBinaryFormat(array, 0, 16));
Console.WriteLine();
// modify a random element in array
Random rand = new Random();
array[rand.Next() % array.Length] = (byte)(rand.Next() % array.Length);
int dstLeft = Console.CursorLeft;
int dstTop = Console.CursorTop;
Console.Write(ToBinaryFormat(array, 0, 16));
Console.MoveBufferArea(srcLeft, srcTop, width, height, dstLeft + 18, dstTop);
}
}
}
Before moving buffer area, we have the output as above.
After moving, the console is as below: