zoukankan      html  css  js  c++  java
  • C# 控制台程序(命令行程序)设置字体颜色,窗口宽高,光标行数

    控制台程序(命令行程序)设置窗口宽度高度,如下代码:

                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中支持的背景色及前景色

            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);
                }

                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();
                }
            }

    计算当前光标所在的行数,针对于Console.BufferHeight的值,代码如下:

                ShowColor();
                int m = Console.CursorTop;//查看当前行号Console.BufferHeight 
                ShowColor();
                int n = Console.CursorTop;
                ShowColor();
                int o = Console.CursorTop;
                Console.ReadKey();
  • 相关阅读:
    根据进程id pid 查容器id
    jenkins 持续集成笔记1 --- 安装配置
    PMM 监控 MySQL 使用钉钉告警
    PMM 监控 MySQL
    docker HealthCheck健康检查
    顶层const和底层const
    Windows下使用VS2017搭建FLTK开发环境
    解决FAT32格式U盘安装win10时0x8007000D错误
    在VS中为C/C++源代码文件生成对应的汇编代码文件(.asm)
    VS2017设置主题和代码字体
  • 原文地址:https://www.cnblogs.com/mq0036/p/3707474.html
Copyright © 2011-2022 走看看