在网上看到了,效果不错,就记下来了
1
using System;2
using System.Collections.Generic;3
using System.Linq;4
using System.Text;5

6
namespace TextProgressing7


{8
class Program9

{10
static void Main(string[] args)11

{12
bool isBreak = false;13
ConsoleColor colorBack = Console.BackgroundColor; ConsoleColor colorFore = Console.ForegroundColor;14

15
//第一行信息 16
Console.WriteLine("*********** jinjazz now working
******");17

18
//第二行绘制进度条背景 19

Console.BackgroundColor = ConsoleColor.DarkCyan; for (int i = 0; ++i <= 25; )
{ Console.Write(" "); } Console.WriteLine(" "); Console.BackgroundColor = colorBack;20

21
//第三行输出进度 22
Console.WriteLine("0%");23
//第四行输出提示,按下回车可以取消当前进度24
Console.WriteLine("<Press Enter To Break.>");25

26
//-----------------------上面绘制了一个完整的工作区域,下面开始工作27

28
//开始控制进度条和进度变化 29
for (int i = 0; ++i <= 100; )30

{31
//先检查是否有按键请求,如果有,判断是否为回车键,如果是则退出循环 32
if (Console.KeyAvailable && System.Console.ReadKey(true).Key == ConsoleKey.Enter)33

{34
isBreak = true;35
break;36
} //绘制进度条进度 37
Console.BackgroundColor = ConsoleColor.Yellow;//设置进度条颜色 38
Console.SetCursorPosition(i / 4, 1);39
//设置光标位置,参数为第几列和第几行 40
Console.Write(" ");//移动进度条 41
Console.BackgroundColor = colorBack;//恢复输出颜色42
//更新进度百分比,原理同上. 43
Console.ForegroundColor = ConsoleColor.Green;44
Console.SetCursorPosition(0, 2);45
Console.Write("{0}%", i);46
Console.ForegroundColor = colorFore;47
//模拟实际工作中的延迟,否则进度太快 48
System.Threading.Thread.Sleep(100);49
}50
//工作完成,根据实际情况输出信息,而且清楚提示退出的信息 51
Console.SetCursorPosition(0, 3);52
Console.Write(isBreak ? "break!!!" : "finished.");53
Console.WriteLine(" ");54
//等待退出 55
Console.ReadKey(true);56
}57
}58

59
}60
