Console in C#

本文介绍了使用C#中的TheConsole类进行控制台操作的方法,包括调整窗口大小、缓冲区高度设置、播放提示音等实用功能,并通过示例展示了如何移动缓冲区内容。

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, 
016));

            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, 
016));            

            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:

转载于:https://www.cnblogs.com/dc10101/archive/2009/03/09/1407147.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值