Progress Sphere

This class displays a translucent, shadowed modal progress dialog. The process whose progress is to be monitored is started as a new thread, allowing the progress sphere to update smoothly. If enabled, the user is given the ability to terminate the process with the ESC key. The code runs on Windows 2000 and later.

1. Structure

All initialisation is performed in the constructor:

CDlgProgress(HWND hParentWnd, PROGRESSFUNC hThreadProc,
             const wchar_t* strTitle, bool bAllowCancel = false);

The parameters in detail:

  • hParentWnd takes a handle to the calling window
  • PROGRESSFUNC supplies the name of the user function that contains the code whose progress is to be monitored
  • strTitle is the text that will display at the top of the window, during the entire execution
  • bAllowCancel tells the class whether to allow premature termination by the user

This method checks whether the progress funtion should continue executing:

bool Continue();

The following function sets the status message of the progress dialog:

void SetProgress(CString strText, double dPos);
  • strText is the status message
  • dPos is the percent completed

This one returns the handle of the dialog:

HWND GetWindowHandle();

The progress function needs to have the following form:

bool ProgressFunction(CDlgProgress* pDlgProgress);

2. Usage

  1. Do the inclusion:
    #include "CDlgProgress.h"
  2. Construct the CDlgProgress object; for example:
    CDlgProgress dlg(m_hWnd, ProgressFunction,
                     L"Lengthy operation being performed...",
                     true);
    
  3. Write a progress function:
    bool ProgressFunction(CDlgProgress* pDlgProgress)
    {
       for(int i = 0; i < 5000; i++)
       {
          if(!pDlgProgress->Continue())
             return 1;
    
          CString strMessage;
          strMessage.Format(L"Current number is %i", i);
          pDlgProgress->SetProgress(strMessage, (double)i/50.0f);
       }
    
       return 0;
    }
    

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read