简单Console进度条

经常使用控制台来写小玩意,总希望有个进度条,各种百度,终于简单实现:

先上进度条帮助类:

 public class ConsoleProgress
    {
        static ConsoleProgress consoleProgress = new ConsoleProgress();
        int top=0;
        int end=0;
        private ConsoleProgress()
        {
            top = Console.CursorTop;
        }
      
         public static ConsoleProgress Intance
        {
            get
            {

                if (consoleProgress == null)
                {
                    lock(new object())
                    {
                        consoleProgress = new ConsoleProgress();
                    }
                }
                return consoleProgress;
            }
        }
        public  void Reset()
        {
            top =Console. CursorTop;
        }
         public void  ShowProgress(int curvalue,string msg)
        {
            if (CursorTop > end && end > 0)
            {
                top = CursorTop;
            }
            Console.SetCursorPosition(100/2-(msg.Length)/2, top);
            //显示提示信息
            Console.WriteLine(msg);
            Console.SetCursorPosition(100 / 2 - ((curvalue.ToString()+"%").Length) / 2, top+1);
            Console.WriteLine("{0}%", curvalue);
            Console.SetCursorPosition(CursorLeft, top+2);
            Console.WriteLine("┃");
            Console.SetCursorPosition(CursorLeft+1, top+2);
            Console.WriteLine(new string('*',curvalue));
            if (curvalue >= 100)
            {
                Console.SetCursorPosition(curvalue+1, top + 2);
                Console.WriteLine("┃");
                Console.SetCursorPosition(100/2-("操作已完成".Length/2) + 1, top + 3);
                Console.WriteLine("操作已完成");
            }
           end= Console.CursorTop;
        }

    }

2.接下来是调用:

Console.WriteLine("开始进度条控制");
          
            for (int i = 0; i++ < 100;) {
               ConsoleProgress.Intance.ShowProgress(i,  "正则测试");
                Thread.Sleep(50);
            }
           ConsoleProgress.Intance.Reset();//此处用于开启一个新的进度条,如果不调用此句,则需要有Console.WriteLine()方法被调用后方可正常在用showProgress,否则进度控制会保留上一次调用时的位置:
           // Console.WriteLine("再次测试。。。。。。。。。。。");
            for (int i = 0; i++ < 100;)
            {
               ConsoleProgress.Intance.ShowProgress(i,  "正则测试");
                Thread.Sleep(50);
            }

            Console.ReadLine();

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

贴出新方法,减小进度条的大小!

 public void  ShowProgress(int curvalue,string msg)
        {
            int total = 30;
            if (CursorTop > end && end > 0)
            {
                top = CursorTop;
            }
            Console.SetCursorPosition(30 / 2-(msg.Length)/2, top);
            //显示提示信息
            Console.WriteLine(msg);
            Console.SetCursorPosition(30 / 2 - ((curvalue.ToString()+"%").Length) / 2, top+1);
            Console.WriteLine("{0}%", curvalue);
            Console.SetCursorPosition(CursorLeft, top+2);
            Console.WriteLine("┃");
            Console.SetCursorPosition(CursorLeft+1, top+2);
            Console.WriteLine(new string('*',curvalue*30/100));
            if (curvalue >= 30)
            {
                Console.SetCursorPosition(curvalue*30/100+1, top + 2);
                Console.WriteLine("┃");
                Console.SetCursorPosition(30 / 2-("操作已完成".Length/2) + 1, top + 3);
                Console.WriteLine("操作已完成");
            }
           end= Console.CursorTop;
        }

 

转载于:https://www.cnblogs.com/gfjin/p/8143957.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值