手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#控制台输出进度和百分比的实例代码
C#控制台输出进度和百分比的实例代码
摘要:复制代码代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;...

复制代码 代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

bool isBreak = false;

ConsoleColor colorBack = Console.BackgroundColor;

ConsoleColor colorFore = Console.ForegroundColor;

//第一行信息

Console.WriteLine("****** now working...******");

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

Console.BackgroundColor = ConsoleColor.DarkCyan;

for (int i = 0; ++i <= 25; )

{

Console.Write(" ");

}

Console.WriteLine(" ");

Console.BackgroundColor = colorBack;

//第三行输出进度

Console.WriteLine("0%");

//第四行输出提示,按下回车可以取消当前进度

Console.WriteLine("<Press Enter To Break.>");

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

//开始控制进度条和进度变化

for (int i = 0; ++i <= 100; )

{

//先检查是否有按键请求,如果有,判断是否为回车键,如果是则退出循环

if (Console.KeyAvailable && System.Console.ReadKey(true).Key == ConsoleKey.Enter)

{

isBreak = true; break;

}

//绘制进度条进度

Console.BackgroundColor = ConsoleColor.Yellow;//设置进度条颜色

Console.SetCursorPosition(i / 4, 1);//设置光标位置,参数为第几列和第几行

Console.Write(" ");//移动进度条

Console.BackgroundColor = colorBack;//恢复输出颜色

//更新进度百分比,原理同上.

Console.ForegroundColor = ConsoleColor.Green;

Console.SetCursorPosition(0, 2);

Console.Write("{0}%", i);

Console.ForegroundColor = colorFore;

//模拟实际工作中的延迟,否则进度太快

System.Threading.Thread.Sleep(100);

}

//工作完成,根据实际情况输出信息,而且清楚提示退出的信息

Console.SetCursorPosition(0, 3);

Console.Write(isBreak ? "break!!!" : "finished.");

Console.WriteLine(" ");

//等待退出

Console.ReadKey(true);

}

}

}

【C#控制台输出进度和百分比的实例代码】相关文章:

C# Winform 实现屏蔽键盘的win和alt+F4的实现代码

c#实现16进制和字符串之间转换的代码

C#制作鹰眼的详细全过程(带注释)实例代码

c# 共享状态的文件读写实现代码

c# 委托和事件实例学习

c#使用linq技术创建xml文件的小例子

C#中常使用进度条的代码

C#仿密保卡功能的简单实现代码

C#打印出正等腰三角形实例代码

C#中让控件全屏显示的实现代码(WinForm)

精品推荐
分类导航