C# 控制檯程序(命令行程序)設置字體顏色,窗口寬高,光標行數

控制檯程序(命令行程序)設置窗口寬度高度,以下代碼:web

            Console.WriteLine(Console.WindowHeight);
            Console.WriteLine(Console.BufferHeight);
            Console.ReadKey();
            Console.Title = "Test";//設置窗口標題
            Console.WindowWidth = 120;
            Console.BufferHeight = 1000;
            Console.WriteLine(Console.WindowWidth);
            Console.WriteLine(Console.WindowHeight);
            Console.WriteLine("---------------------");
            Console.WriteLine(Console.BufferWidth);
            Console.WriteLine(Console.BufferHeight);

設置窗口字體顏色和背景顏色:字體

            Console.BackgroundColor = ConsoleColor.Blue; //設置背景色
            Console.ForegroundColor = ConsoleColor.White; //設置前景色,即字體顏色
            Console.WriteLine("第一行白藍.");
            Console.ResetColor(); //將控制檯的前景色和背景色設爲默認值
            Console.BackgroundColor = ConsoleColor.Green;
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            string str = "第三行 綠暗綠";
            Console.WriteLine(str.PadRight(Console.BufferWidth - (str.Length % Console.BufferWidth))); //設置一整行的背景色
            Console.ResetColor();

 

 

//顯示出console中支持的背景色及前景色spa

        static void ShowColor()
        {
            Type type = typeof(ConsoleColor);
            Console.ForegroundColor = ConsoleColor.White;
            foreach (string name in Enum.GetNames(type))
            {
                Console.BackgroundColor = (ConsoleColor)Enum.Parse(type, name);
                Console.WriteLine(name);
            }
命令行

            Console.BackgroundColor = ConsoleColor.Black;
            foreach (string name in Enum.GetNames(type))
            {
                Console.ForegroundColor = (ConsoleColor)Enum.Parse(type, name);
                Console.WriteLine(name);
            }
code

            foreach (string bc in Enum.GetNames(type))
            {
                Console.BackgroundColor = (ConsoleColor)Enum.Parse(type, bc);
                foreach (string fc in Enum.GetNames(type))
                {
                    Console.ForegroundColor = (ConsoleColor)Enum.Parse(type, fc);
                    Console.WriteLine("bc="+bc+",fc="+fc);
                }
                Console.WriteLine();
            }
        }
orm

計算當前光標所在的行數,針對於Console.BufferHeight的值,代碼以下:blog

            ShowColor();
            int m = Console.CursorTop;//查看當前行號Console.BufferHeight 
            ShowColor();
            int n = Console.CursorTop;
            ShowColor();
            int o = Console.CursorTop;
            Console.ReadKey();
相關文章
相關標籤/搜索