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;
    }
    


Downloads

Comments

  • Good Start.

    Posted by AliRafiee on 07/24/2009 03:50pm

    This is a nice control, but has a few drawbacks. It would be really nice if it was scalebale, since the size is fixed it would be difficult to use. The other thing is the fact that you require the user to have a static call back which will make it difficult to program around. It would be nice it your control would just plug into any dialog, that way the user can put their long process into a worker thread, and simply show a dialog with your control in it while the thread is working on the long process. I guess All of this can be down by the readers of the article in a few hours, but again would have been nice if it was there.

    • Good Start.

      Posted by tari on 07/25/2009 04:12am

      Hello Ali, You have a point here, but this was intended more as a demo rather than a complete solution. (Un)fortunately I've completely moved on to Linux since I wrote it, so it's up to you and the community to shape the code to suit your specific needs. Feel free to copy/paste parts of the code and experiment, you can even alter it and post it back here, if you wish so. The project is 100% my coinage, I hold no copyright - you can freely take it apart and use it any way you seem appropriate.

      Reply
    Reply
  • Unhandled exception

    Posted by glenn.wasserman on 03/22/2007 08:47am

    I'm using this in an existing MFC app and I seem to get random unhandled exceptions...where the debugger breaks inside AfxCallWndProc and the pWnd pointer passed into the routine is null. Mostly these exceptions happen when I shutdown the app but they also can happen when I execute the code that uses this module. Note that I can't get the exception to happen on a debug build - it only happens in my "release" build

    • Unhandled exception

      Posted by glenn.wasserman on 03/26/2007 08:40pm

      I figured out the problem - in the code that detects that ESC was hit, it was calling WaitForSingleObject with a 5 second wait, after which it was calling EndDialog. What I found was that sometimes, a short time after this, my code was returning and then your code was calling EndDialog a second time. So I eliminated the EndDialog in the callback and rely on my code returning back into your code to call EndDialog just once.

      Reply
    • Re: Unhandled exception

      Posted by tari on 03/26/2007 05:45am

      Hello Glenn,
      
      What you're describing here could be caused by almost anything, AfxCallWndProc can break for various reasons. Have you tried commenting out the ProgressSphere code from your program and see if you still get errors? If all else fails, you could send me your source code to have a look at (unless it's a top secret missile guiding application, of course).

      Reply
    Reply
  • demo application

    Posted by mop65715 on 07/07/2006 04:29pm

    I'm executing the demo application and it appears that once the application starts there's no way to stop the application - short of using task manager. Am I correct?

    • demo application

      Posted by tari on 07/08/2006 03:15am

      Hello, Michele. If cancel is enabled in the constructor, you should be able to stop it by pressing the ESC key when the window has focus.

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • It's no secret what keeps CIOs up at night. Mobile, cloud, data, security, and social have become the "five imperatives," the drivers of business progress, innovation, and competitive differentiation. Business leaders around the world want to hear how other companies are succeeding. How are they applying the latest technologies? How did they get started? What outcomes are they achieving? Read this online magazine for success stories from organizations like the NBA, Pfizer, and San Jose State University as they …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds