【c#】一些控制台知识

Console类 输出改变颜色

Console.ForegroundColor = ConsoleColor.Blue;  
Console.WriteLine("Hello, color text!");  
Console.ForegroundColor = ConsoleColor.Red;  
Console.WriteLine("Hello, color text!");  
Console.ReadKey();  

改变背景

Console.BackgroundColor = ConsoleColor.DarkYellow;  
            Console.WriteLine("asdas");  

清空控制台输出

Console.Clear();

线程休眠

System.Threading.Thread.Sleep(1000);    //1000表示一秒

控制台变颜色

namespace demo
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteCharacterStrings(24, 24, true);


            Console.Clear();


            Console.ReadKey();
        }
        private static void WriteCharacterStrings(int start, int end,
                                             bool changeColor)
        {
            for (int ctr = start; ctr <= end; ctr++)
            {
                if (changeColor)
                    Console.BackgroundColor = (ConsoleColor)((ctr - 1) % 16);

            }
        }
    }
}

获取键盘输入

bool tag = true;
            do
            {
                ConsoleKeyInfo info = Console.ReadKey();
                switch (info.Key)
                {
                    case ConsoleKey.E:
                        Console.WriteLine("exit");
                        tag = false;
                        break;
                    case ConsoleKey.UpArrow:
                        Console.WriteLine("Up");
                        break;
                    case ConsoleKey.DownArrow:
                        Console.WriteLine("Down");
                        break;
                    case ConsoleKey.LeftArrow:
                        Console.WriteLine("Left");
                        break;
                    case ConsoleKey.RightArrow:
                        Console.WriteLine("Right");
                        break;
                    default:
                        Console.WriteLine(info.Key);
                        break;
                }
            } while (tag);

光标位置改变

Console.SetCursorPosition(origCol+x, origRow+y);

控制台颜色:


【结束控制台】

如果代码在主函数Main里,直接用return就可以了。
如果代码在其他函数里,退出控制台用Environment.Exit(0);

暂时知道怎么多!

https://msdn.microsoft.com/zh-cn/library/system.console(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#属性

猜你喜欢

转载自blog.csdn.net/qq_41138935/article/details/80802581
今日推荐