【转】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;
}
}
}
}