使用C#的控制台cmd输出进度条的Demo,Class类已经写好,可直接拿去使用

本博客文章   未经允许不得转载不得转售或其他商业用途 但您可以随意使用到您的个人项目中


public class ConsoleProcessBar
    {
        int left, top;//进度条在cmd中的位置
        int width;//进度条在cmd中用多少个字符来表示,注意这个宽度是字符多少个不是包含了[]尖括号以及文本的宽度
        bool showing = false;//是否正在显示中,如果是显示中,下一次更新进度的时候直接更新里面的字符 不用全部更新
        int textoffset = 2;//文本内容在进度条的右侧扩展多少个距离后显示
        string head = "[";//进度条的头部内容
        string end = "]";//进度条的尾部内容
        int fullLineLength;//进度条首位总共长度,不包含text
        int currentMsgLength;///当前的文本的长度,用于下次在显示文本的时候把之前的擦除

        
        /// <summary>
        /// 进度条显示文本的方式 屏蔽  百分比  当前/总数
        /// </summary>
        public enum PercentTextShowMode { HIDE, Percent, CurrentAndTotal };
        /// <summary>
        /// 百分比文字的显示办法  不显示,百分比,当前/总数
        /// </summary>
        public PercentTextShowMode TextShowMode { get; set; }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="left">进度条在cmd中左坐标</param>
        /// <param name="top">进度条在cmd中上坐标</param>
        /// <param name="barViewWidth">进度条显示的时候用多少个小星星也就是像素来展示</param>
        /// <param name="textmode">显示文本的方式</param>
        public ConsoleProcessBar(int left, int top, int barViewWidth, PercentTextShowMode textmode)
        {
            this.TextShowMode = textmode;
            this.width = barViewWidth;
            if (this.width < 10)
            {
                this.width = 10;
            }
            this.left = left;
            this.top = top;
        }
        /// <summary>
        /// 更新进度条的位置.
        /// </summary>
        /// <param name="current">当前位置</param>
        /// <param name="total">总位置</param>
        /// <param name="msg">要显示的信息</param>
        public void SetPos(int current, int total, string msg)
        {
            #region 计算当前位置
            int currentviewpos = (int)Math.Round((current+1) * 1f / total * width);
            #endregion
            //记录在没显示进度条的时候光标的位置
            int oldleft = Console.CursorLeft;
            int oldtop = Console.CursorTop;
            //如果没有在显示,那就显示出来进度条
            if (showing == false)
            {
                //回到进度条初始位置
                Console.SetCursorPosition(left, top);
                //显示正在处理中的进度
                //已经处理的百分比用星星表示
                string doneProcessChars = "".PadRight(currentviewpos, '*');
                string emtpyProcessCharts = "".PadRight(width - currentviewpos, ' ');
                //整个这一行的内容,0参数是已经处理的,后面是没处理的
                string alllineChars = string.Format("{0}{1}{2}{3}",head,doneProcessChars,emtpyProcessCharts,end);
                fullLineLength = alllineChars.Length;
                ConsoleColor oldcolor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.Write(alllineChars);
                Console.ForegroundColor = oldcolor;
                showing = true;
            }
            else
            {
                //return;
                //如果在显示直接更新进度即可 
                //设置到进度条的需要更新的位置
                Console.SetCursorPosition(left + 1,top);
                string doneProcessChars = "".PadRight(currentviewpos, '*');
                ConsoleColor oldcolor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.DarkGreen;


                Console.Write(doneProcessChars);


                
                Console.ForegroundColor = oldcolor;

            }
            #region 显示进度文字信息
            string text = "";
            switch (this.TextShowMode)
            {
                case PercentTextShowMode.HIDE:
                    break;
                case PercentTextShowMode.Percent:
                    text = string.Format("{0}%", (int)Math.Round((current+1)*100f/total));
                    break;
                case PercentTextShowMode.CurrentAndTotal:
                    text = string.Format("{0}/{1}",current+1,total);
                    break;
                default:
                    break;
            }
            Console.SetCursorPosition(fullLineLength+textoffset+left, top);
            Console.Write(text);
            #endregion
            #region 显示附加文字信息
            //是不是需要擦除
            int l = Encoding.UTF8.GetByteCount(msg);
            if (currentMsgLength>0 && l<currentMsgLength)
            {
                msg = msg.PadRight(currentMsgLength, ' ');
            }
            currentMsgLength = l;
            if (string.IsNullOrEmpty(msg) == false)
            {
                Console.SetCursorPosition(this.left, Console.CursorTop + 1);
                Console.Write(msg);
            }
            #endregion
            //回到原来的显示位置
            Console.SetCursorPosition(oldleft, oldtop);
        }
   
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Afterwards_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值