Delphi调用IProgressDialog接口示例

来源:互联网 发布:hadoop 2.8.0 windows 编辑:程序博客网 时间:2024/05/16 02:59
IProgressDialog是Windows外壳提供的用于显示进度的对话框,包含文本提示及进度条,还可自动进行时间估算。
该接口主要包含以下方法:
HasUserCancelledChecks whether the user has canceled the operation.SetAnimationSpecifies an Audio-Video Interleaved (AVI) clip that runs in the dialog box.SetCancelMsgSets a message to be displayed if the user cancels the operation.SetLineDisplays a message.SetProgressUpdates the progress dialog box with the current state of the operation.SetProgress64Updates the progress dialog box with the current state of the operation.SetTitleSets the title of the progress dialog box.StartProgressDialogStarts the progress dialog box.StopProgressDialogStops the progress dialog box and removes it from the screen.TimerResets the progress dialog box timer to zero.
 
具体用法请参照MSDN,参照下面源代码就可以明白其用法,WIN7+Delphi XE3 调试通过。
 
以下为主要代码片断,主要步骤为:
1. 通过CreateComObject创建COM对象得到IProgressDialog接口
2. SetTitle设置标题,SetLine设置各行显示文本(可设置的有三行,行号从1开始)
3. StartProgressDialog显示对话框
4. 在任务执行过程中调用SetProgress设置进度条(通过HasUserCancelled检测是否用户取消)
5. StopProgressDialog关闭对话框,释放接口
 
procedure TFormMain.btnStartClick(Sender: TObject);var  pNil: Pointer;  I: Integer;begin  ProgressDialog := CreateComObject(CLSID_ProgressDialog) as IProgressDialog;  ProgressDialog.SetTitle(PWideChar(Caption));  ProgressDialog.SetProgress(0, max);  ProgressDialog.SetLine(1, PWideChar(Format('进行%d次循环', [max])), True, pNil);  ProgressDialog.StartProgressDialog(Handle, nil, PROGDLG_AUTOTIME or    PROGDLG_NOMINIMIZE, pNil);  for I := 0 to max do  begin    if ProgressDialog.HasUserCancelled then    begin      Break;    end;    ProgressDialog.SetProgress(I, max);    ProgressDialog.SetLine(2, PWideChar(Format('当前循环到:%d', [I])), True, pNil);    Application.ProcessMessages;  end;  ProgressDialog.StopProgressDialog;  ProgressDialog := nil;end;
 
要进行时间自动估算,使用PROGDLG_AUTOTIME标志,并且时间会自动显示在Line3,所以SetLine设置文本时,不要使用第3行.
 
源代码下载:http://www.ctdisk.com/file/11965303

 
 
原创粉丝点击