C# 控制台进度条

在后端开发总经常会遇到需要可视化进度的情况,下面我就给给大家讲解两种进度条展示方式,实现起来也非常简单,废话不多说咱们直接上代码。

效果展示

下面两种方法主要展示的是两种进度条展示模式,第一种是彩色进度条,第二种是字符进度条

  • 彩色进度条执行效果在这里插入图片描述
  • 字符进度条执行效果
    在这里插入图片描述

调用方法


  ProgressBar progressBar = new ProgressBar(Console.CursorLeft, Console.CursorTop, 50, ProgressBarType.Multicolor);
            Compress(100, progressBar.Dispaly);
            Console.WriteLine();
            ProgressBar progressBara = new ProgressBar(Console.CursorLeft, Console.CursorTop, 50, ProgressBarType.Character);
            Compress(100, progressBara.Dispaly);
            Console.WriteLine();

进度计算方法

这个方法主要用于控制进度条进度。Thread.Sleep使用于模拟耗时操作,在实际开发中可以将其替换成业务代码

  public static void Compress(int val, Func<int, int> dispalyProgress = null) {

            for (int i = 0; i <=val; i++)
            {
                if (dispalyProgress != null) { dispalyProgress(Convert.ToInt32((i / (val * 1.0)) * 100)); }
             	Thread.Sleep(1000);
            }
        }

打印进度条方法

 namespace ProgressBarSolution
    {
        /// <summary>
        /// 进度条类型
        /// </summary>
        public enum ProgressBarType
        {
            /// <summary>
            /// 字符
            /// </summary>
            Character,
            /// <summary>
            /// 彩色
            /// </summary>
            Multicolor
        }

        public class ProgressBar
        {

            /// <summary>
            /// 光标的列位置。将从 0 开始从左到右对列进行编号。
            /// </summary>
            public int Left { get; set; }
            /// <summary>
            /// 光标的行位置。从上到下,从 0 开始为行编号。
            /// </summary>
            public int Top { get; set; }

            /// <summary>
            /// 进度条宽度。
            /// </summary>
            public int Width { get; set; }
            /// <summary>
            /// 进度条当前值。
            /// </summary>
            public int Value { get; set; }
            /// <summary>
            /// 进度条类型
            /// </summary>
            public ProgressBarType ProgressBarType { get; set; }


            private ConsoleColor colorBack;
            private ConsoleColor colorFore;


            public ProgressBar() : this(Console.CursorLeft, Console.CursorTop)
            {

            }

            public ProgressBar(int left, int top, int width = 50, ProgressBarType ProgressBarType = ProgressBarType.Multicolor)
            {
                this.Left = left;
                this.Top = top;
                this.Width = width;
                this.ProgressBarType = ProgressBarType;

                // 清空显示区域;
                Console.SetCursorPosition(Left, Top);
                for (int i = left; ++i < Console.WindowWidth;) { Console.Write(" "); }

                if (this.ProgressBarType == ProgressBarType.Multicolor)
                {
                    // 绘制进度条背景;
                    colorBack = Console.BackgroundColor;
                    Console.SetCursorPosition(Left, Top);
                    Console.BackgroundColor = ConsoleColor.DarkCyan;
                    for (int i = 0; ++i <= width;) { Console.Write(" "); }
                    Console.BackgroundColor = colorBack;
                }
                else
                {
                    // 绘制进度条背景;
                    Console.SetCursorPosition(left, top);
                    Console.Write("[");
                    Console.SetCursorPosition(left + width - 1, top);
                    Console.Write("]");
                }
            }

            public  int Dispaly(int value)
            {
                return Dispaly(value, null);
            }

            public  int Dispaly(int value, string msg)
            {
                if (this.Value != value)
                {
                    this.Value = value;

                    if (this.ProgressBarType == ProgressBarType.Multicolor)
                    {
                        // 保存背景色与前景色;
                        colorBack = Console.BackgroundColor;
                        colorFore = Console.ForegroundColor;
                        // 绘制进度条进度
                        Console.BackgroundColor = ConsoleColor.Yellow;
                        Console.SetCursorPosition(this.Left, this.Top);
                        Console.Write(new string(' ', (int)Math.Round(this.Value / (100.0 / this.Width))));
                        Console.BackgroundColor = colorBack;

                        // 更新进度百分比,原理同上.
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.SetCursorPosition(this.Left + this.Width + 1, this.Top);
                        if (string.IsNullOrWhiteSpace(msg)) { Console.Write("{0}%", this.Value); } else { Console.Write(msg); }
                        Console.ForegroundColor = colorFore;
                    }
                    else
                    {
                        // 绘制进度条进度
                        Console.SetCursorPosition(this.Left + 1, this.Top);
                        Console.Write(new string('*', (int)Math.Round(this.Value / (100.0 / (this.Width - 2)))));
                        // 显示百分比
                        Console.SetCursorPosition(this.Left + this.Width + 1, this.Top);
                        if (string.IsNullOrWhiteSpace(msg)) { Console.Write("{0}%", this.Value); } else { Console.Write(msg); }
                    }
                }
                return value;
            }
        }
    }

执行效果

在这里插入图片描述

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐