【转】让控制台程序也能拥有进度条

【转】https://blog.csdn.net/hu2008yinxiang/article/details/7840295

using System;
using System.Threading;

namespace ConsoleAppProgressBar
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            ConsoleProgressBar consoleProgressBar = new ConsoleProgressBar();

            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(500);

                consoleProgressBar.Update((i+1) * 0.01);
                
            }


            Console.WriteLine("end");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleAppProgressBar
{
    public class ConsoleProgressBar
    {
        /// <summary>
        /// 构造一个指定的进度条
        /// </summary>
        /// <param name="foreGroundColor">进度条颜色</param>
        /// <param name="posTop">行位置</param>
        /// <param name="posLeft">列位置</param>
        /// <param name="cellChar">单元格样式</param>
        public ConsoleProgressBar(System.ConsoleColor foreGroundColor, int posTop, int posLeft, char cellChar)
        {
            ForegroundColor = foreGroundColor;
            PositionTop = posTop;
            PositionLeft = posLeft;
            CellChar = cellChar;
        }
        /// <summary>
        /// 构造一个默认的进度条
        /// 其颜色为红色
        /// 位置为当前位置
        /// 单元格样式为'='
        /// </summary>
        public ConsoleProgressBar() : this(ConsoleColor.Red, Console.CursorTop, Console.CursorLeft, '=')
        {
        }
        /// <summary>
        /// 行位置
        /// </summary>
        public int PositionTop { get; set; }
        /// <summary>
        /// 列位置
        /// </summary>
        public int PositionLeft { get; set; }
        /// <summary>
        /// 单元格样式
        /// </summary>
        public char CellChar { get; set; }
        private double value = 0;
        /// <summary>
        /// 进度值
        /// </summary>
        public double Value
        {
            get { return value; }
            set { Update(value); }
        }
        /// <summary>
        /// 前景色
        /// </summary>
        public System.ConsoleColor ForegroundColor { get; set; }


        /// <summary>
        /// 刷新进度
        /// </summary>
        /// <param name="percent">百分比(0.01-1)</param>
        public void Update(double percent)
        {
            if (percent < 0) percent = 0;
            if (percent > 1) percent = 1;
            this.value = percent;
            int width = System.Console.BufferWidth - PositionLeft;
            //XXX% 7
            //[]   2
            //     1
            //
            width = width - 11;
            string ps = "";
            int per = (int)(percent * width);
            for (int i = 0; i < width; ++i)
            {
                if (per >= i && per != 0)
                    ps += CellChar;
                else
                    ps += " ";
            }
            lock (System.Console.Out)
            {
                //存档
                int post = System.Console.CursorTop;
                int posl = System.Console.CursorLeft;
                bool curv = System.Console.CursorVisible;
                System.Console.CursorVisible = false;
                System.ConsoleColor fc = System.Console.ForegroundColor;
                System.ConsoleColor bc = System.Console.BackgroundColor;
                //开画
                System.Console.CursorTop = PositionTop;
                System.Console.CursorLeft = PositionLeft;
                System.Console.ForegroundColor = System.ConsoleColor.Cyan;
                System.Console.Write("[");
                System.Console.ForegroundColor = ForegroundColor;
                System.Console.Write(ps);
                System.Console.ForegroundColor = System.ConsoleColor.Cyan;
                System.Console.Write("] ");
                System.Console.ForegroundColor = ForegroundColor;
                System.Console.Write("{0,6:##0.00}", (percent * 100));
                System.Console.ForegroundColor = System.ConsoleColor.Cyan;
                System.Console.Write("%");
                //System.Console.WriteLine();
                //画完
                //恢复
                System.Console.ForegroundColor = fc;
                System.Console.BackgroundColor = bc;
                System.Console.CursorVisible = curv;
                System.Console.CursorLeft = posl;
                if (post == PositionTop)
                {
                    System.Console.CursorTop += 1;
                }
                else System.Console.CursorTop = post;
            }
        }
    }
 
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值