C# 控制台进度条

这篇博客展示了如何在控制台应用中使用ProgressBar类来创建和更新进度条。代码包括了两种类型的进度条,即彩色和字符型,并通过Compress方法动态显示压缩过程的进度。示例中,ProgressBar类接收光标位置、宽度和类型作为参数,同时提供了Dispaly方法来更新进度条和显示百分比。

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

执行效果

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李袁明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值