A New Progress Bar for All Occassions

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Environment: VC6, VC7, MFC

Description

This article describes a CStatic-derived Progress bar that allows “busy” times with no defined end to be indicated with more than just an hourglass cursor.

Adding the Code to Your project

The CKCBusyProgress control can be subclassed within a dialog by following these steps:

  1. Insert the .h and .cpp files into your project.
  2. Add a static control to your dialog resource.
  3. Add a member variable for the static control (eg m_ctlBProgress).
  4. Change the variable declaration from:
  5. CStatic m_ctlBProgress;

    to:

    CKCBusyProgress m_ctlBProgress;
  6. et voilá! You have the control embedded!

Public Functions

void SetNumSteps(int nNumSteps)

Sets the number of visible blocks in the control


int GetNumSteps()

Gets the number of visible blocks in the control


void SetCurPos(int nCurPos)

Sets the current position of the marker


int GetCurPos()

Gets the current position of the marker


void SetInterBlockPadding(int nPadding)

Sets the number of pixels between each block


int GetInterBlockPadding()

Gets the number of pixels between each block


void SetSpeed(int nSpeed)

Sets the speed (in milliseconds) of visual updates when the control is in BPC_MODE_BUSY mode


int GetSpeed()

Gets the speed (in milliseconds) of visual updates when the control isin BPC_MODE_BUSY mode


bool IsRunning()

Indicates whether the control is currently indicating a busy state (the control must be in BPC_MODE_BUSY mode)


void SetMode(int nMode = BPC_MODE_BUSY)

Sets the mode of the control
Valid parameters are:







BPC_MODE_BUSY 0x00000001 Puts the control into the BusyProgress mode (no range needed)
BPC_MODE_PROGRESS 0x00000002 Puts the control into the ProgressBar mode (can set a range—default is 0 .. 100)


int GetMode()

Gets the current mode of the control


void SetRange(int nLower, int nUpper)

Sets the lower and upper bounds of the control for when it is in BPC_MODE_PROGRESS mode


void GetRange(int& nLower, int& nUpper)

Gets the lower and upper bounds of the control


void Recalc()

Request the control to recalculate its internal visual aspects and ratios


void Reset()

Call this to reset the control to the state it was when it was first created


void Start()

Request the control to start its “busy” mode. This will only work if the control’s mode is BPC_MODE_BUSY


void End()

Request the control to end its “busy” mode. This will only work if the control’s mode is BPC_MODE_BUSY and the Start() function was called prior to this


void StepIt()

Call this function to manually “step” the control marker. This function works in both modes. When called in BPC_MODE_BUSY mode, the function will automatically cause the marker to change direction once it has reached either left or right control edge


COLORREF GetColBkg()

Gets the colour of the background


void SetColBkg(COLORREF col)

Sets the colour of the background


COLORREF GetColBlockFace()

Gets the fill colour of the face of normal blocks


void SetColBlockFace(COLORREF col)

Sets the fill colour of the face of normal blocks


COLORREF GetColBlockEdge()

Gets the pen colour of the border of each normal block


void SetColBlockEdge(COLORREF col)

Sets the pen colour of the border of each normal block


COLORREF GetColBlockFaceHi()

Gets the fill colour of the face of highlighted blocks


void SetColBlockFaceHi(COLORREF col)

Sets the fill colour of the face of highlighted blocks


COLORREF GetColBlockEdgeHi()

Gets the pen colour of the border of each highlighted block


void SetColBlockEdgeHi(COLORREF col)

Sets the pen colour of the border of each highlighted block


Overridable Functions

virtual void DrawBackground(CDC& dc, CRect& rect)

Override this function if you want different background drawing logic. The CRect rect parameter contains the dimensions of the control’s client area


virtual void DrawBlock(CDC& dc, CRect& rect)

Override this function if you want to change the drawing logic of a standard block in the control.
The CRect rect parameter contains the dimensions of the block being drawn


virtual void DrawHiliteBlock(CDC& dc, CRect& rect)

Override this function if you want to change the drawing logic of a highlighted block in the control.
The CRect rect parameter contains the dimensions of the block being drawn


virtual void DrawPartialBlock(CDC& dc, CRect& rect)

Override this function if you want to change the drawing logic of a partially highlighted block in the control.
The CRect rect parameter contains the dimensions of the block being drawn


… and the Control Responds to Messages …

The following messages are defined for interaction with the control via the SendMessage() or PostMessage() functions:











































Message

WPARAM

LPARAM

Corresponding function

BPM_SETNUMSTEPS Number of Steps (int) N/A SetNumSteps(int nNumSteps)
BPM_SETCURPOS Current Marker Position(int) N/A SetCurPos(int nCurPos)
BPM_SETIBPAD Interblock padding in pixels (int) N/A SetInterBlockPadding(int nPadding)
BPM_SETSPEED Speed of busy mode in milliseconds (int) N/A SetSpeed(int nSpeed)
BPM_SETRANGE Lower range of the progress bar (int) Upper range of the progress bar (int) SetRange(int& nLower, int& nUpper)
BPM_SETMODE Mode of the control (int) N/A SetMode(int nMode)
BPM_STARTBUSY N/A N/A Start()
BPM_ENDBUSY N/A N/A End()
BPM_STEPIT N/A N/A StepIt()

Known Issues/To-Do List

  • Must implement an orientation flag to indicate horizontal or vertical display
  • Contemplating adding a tooltip to indicate “percentage” complete when the control is in BPC_MODE_PROGRESS mode
  • Increase the message list (BPM_XXXXX) to enable setting ALL parameters (such as colours)

Conclusion

If you want an alternative to the stock standard Windows CProgressCtrl control, give this one a spin. Let me know if you like it.

Downloads


Download demo project – 51 Kb


Download source – 5 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read