一、ProgressMeter类
通过使用Autodesk.AutoCAD.Runtime命名空间中的ProgressMeter来显示,需要添加对acdbdmgd.dll、acmgd.dll、DotNetARX.dll的引用,并将“复制本地”属性改为False.
//设置进度条对象
Autodesk.AutoCAD.Runtime.ProgressMeter pm = new Autodesk.AutoCAD.Runtime.ProgressMeter();
//设置进度条最大数目
pm.SetLimit(totalCount);
//进度条开始工作,并显示提示文字
pm.Start(chkName);
//干活
pm.MeterProgress();
System.Windows.Forms.Application.DoEvents();
//干活
System.Windows.Forms.Application.DoEvents();
//进度条停止
pm.Stop();
但是这种方法会有明显的卡顿现象,而且需要使用DoEvents来刷新进度条,DoEvent非常影响效率,即便每隔5000次再调用1次DoEvents都不行,进度条每隔6%刷新1次,非常卡;
if (curCount == totalCount || curCount % 5000 == 0)
{
System.Windows.Forms.Application.DoEvents();
}
二、Utils类
Autodesk.AutoCAD.Internal命名空间中的Utils类提供了一种创建进度条的方法:
SetApplicationStatusBarProgressMeter(string log , int minimun, int maximum)
其中log为进度条提示信息,minimum为进度条最小位置,maximum为进度条最大位置。
//创建消息过滤类
MessageFilter filter=new MessageFilter();
//为程序添加消息过滤
System.Windows.Forms.Application.AddMessageFilter(filter);
bool esc=false;//标识是否按下了Esc键
using (Transaction trans=db.TransactionManager.StartTransaction())
{
// 干活
//进度条开始工作并设置进度条前要显示的提示文字
Utils.SetApplicationStatusBarProgressMeter("开始进行检查", 0, points.Count);
//设置进度
Utils.SetApplicationStatusBarProgressMeter(i);
//如果用户按下了Esc键,则结束遍历
if (filter.KeyName == Keys.Escape)
{
esc = true;
break;
}
//让AutoCAD在长时间任务处理时仍然能接收消息
System.Windows.Forms.Application.DoEvents();
Utils.RestoreApplicationStatusBar();//恢复状态栏
if (esc) trans.Abort();//如果按下了Esc键,则放弃所有的更改
else trans.Commit(); //否则程序能完成所有点的变色工作,提交事务处理
}
//移除对按键消息的过滤
System.Windows.Forms.Application.RemoveMessageFilter(filter);
三、ProgressManager类
使用上面 两个命令,当数据量非常大时,会出现非常明现的闪烁现象,可以使用DotNetARX基本类来解决这个问题。
ProgressManager manager = new ProgressManager(chkName);
manager.SetTotalOperations(totalCount);
//干活
manager.Tick();
//干活
manager.Dispose();
使用manager**非常平滑**,不卡顿。深得我心。