Console in C #

Source: Internet
Author: User

The Console class also recils 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 restrict strate 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. Required wwidth = XXX;
One thing you shoshould pay attention on:
The specified wwidth 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 maximize our console window:
Console. Required wwidth = Console. LargestWindowWidth;
Console. Transport wheight = 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 every y you when the job is completed.

You can also play a beep song by its overload.

Clear ()
When we run a application which shoshould 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 shocould 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 shoshould 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;
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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.