65.9K
CodeProject 正在变化。 阅读更多。
Home

CEstimateTimeStatic - 用于估算剩余时间的类

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.69/5 (8投票s)

2003年2月7日

2分钟阅读

viewsIcon

51436

downloadIcon

1111

一个简单而有效的类, 用于估算进程执行的剩余时间

Sample Image - CEstimateTimeStatic.jpg

概述

本文介绍了一个用于估算和显示剩余时间的 CStatic 派生类。它还提供了颜色功能(非常感谢 Robert Brault 的 CColorStatic 类)。使用此类与使用 CProgressCtrl 类似 - 您设置进程大小,然后随着进程的执行,您偏移当前位置,并且知道进程中的当前位置和所花费的时间量,它会计算出直到进程结束的剩余时间。该类不是每次偏移时都估算剩余时间,估算是每 1 秒完成的CEstimateTimeStatic 也适用于可能被中断的进程(例如,向用户询问确认)然后恢复(显示确认和用户做出选择后恢复进程之间的时间间隔不计入,因为这段时间未用于执行该进程)。

用法

  1. EstimateTimeStatic.hEstimateTimeStatic.cpp 添加到您的项目中。
  2. 在对话框上放置一个 Static/label 控件。
  3. 为特定的静态控件添加一个成员变量。 给它命名(例如 m_sEstimateTimeLeft)并选择 Control 类型的类别。 这将自动使变量类型为 CStatic
  4. 打开您的类并在顶部添加 #include "EstimateTimeStatic.h" 行以包含 CEstimateTimeStatic 类的声明。 在头文件中将类 CStatic 替换为 CEstimateTimeStatic
         //{{AFX_DATA(CEstimateTimeSampleDlg)
        enum { IDD = IDD_ESTIMATETIMESAMPLE_DIALOG };
        CEstimateTimeStatic m_sEstimateTimeLeft;
    
    	//}}AFX_DATA
  5. 在您开始执行指定的进程之前,初始化 m_sEstimateTimeLeft

    假设您要复制一些总大小为 100MB 的文件

    • 设置要显示的消息
    • 设置进程大小
       m_sEstimateTimeLeft.SetMessageText("Estimated time left : "); 
       //message to be displayed
       m_sEstimateTimeLeft.SetProcessSize(100*1024); 
       //set process size in KB for better resolution
  6. 调用函数 m_sEstimateTimeLeft.StartProcess () 并开始复制文件
    void CYourClass::CopySomeFiles(){
       ...
       ...
       m_sEstimateTimeLeft.StartProcess ();
       ...
       //start copying
       for (int nCount = 1;nCount <= nFilesNumber ; nCount++)
    
    	{ //copy file m_sEstimateTimeLeft.OffsetProcessPosition(file size(in kilobytes)); if (
    	  //we need to display some confirmation
    	  //) { m_sEstimateTimeLeft.PauseProcess(); 
    	  //display confirmation (the confirmation window is supposed to be modal,
    	  //so the execution of the process stops) m_sEstimateTimeLeft.ResumeProcess(); } } }
  7. 不要忘记调用 m_sEstimateTimeLeft.StopProcess()
    void  CYourClass::CopySomeFiles()
       {
         ...
         ...
         m_sEstimateTimeLeft.StopProcess (); 
       }

致谢

  • Robert Brault - 用于使用他的 CColorStatic 类的代码

许可证

本文未附加明确的许可证,但可能在文章文本或下载文件本身中包含使用条款。如有疑问,请通过下面的讨论区联系作者。

作者可能使用的许可证列表可以在此处找到。

© . All rights reserved.