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

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds