Console in C#

來源:互聯網
上載者:User

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;
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:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.