Click here to Skip to main content
15,867,939 members
Articles / Desktop Programming / MFC
Article

A new progress bar for all occassions...

Rate me:
Please Sign up or sign in to vote.
4.95/5 (66 votes)
4 May 2003CPOL6 min read 219.9K   5.5K   115   78
A CStatic derived Progress bar control that can indicate "busy states" effectively and double up as a good looking progress bar

Sample Image - kcbusyprogress.jpg

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 :
    CStatic m_ctlBProgress;
    to:
    CKCBusyProgress m_ctlBProgress;
  5. et voila! 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_BUSY0x00000001Puts the control into the BusyProgress mode (no range needed)
BPC_MODE_PROGRESS0x00000002Puts 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
Remember to call UseSysColBkg(false) if you do not want changes in the system dialog colour to affect the control's 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

void UseSysColBkg(bool bUse = true)

Indicates to the control whether its background is being drawn using the COLOR_3DFACE system colour or not. If bUse = true, the control will respond to WM_SYSCOLORCHANGE messages and update itself according to changes in system colours

void SetBusyType(int nType = BPC_BUSY_PINGPONG)

Indicates the type of motion employed when the control is running in BPC_MODE_BUSY mode. The 3 options are: BPC_BUSY_PINGPONG, BPC_BUSY_LTR, BPC_BUSY_RTL

int GetBusyType()

Gets the current type of motion that the control is using when in BPC_MODE_BUSY mode.

void SetBusyFill(int nFill = BPC_BUSYFILL_BLOCK)

Indicates the type of fill method used when the control is running in BPC_MODE_BUSY mode. The options are : BPC_BUSYFILL_BLOCK and BPC_BUSYFILL_SMOOTH

int GetBusyFill()

Gets the type of fill method used when the control is running in BPC_MODE_BUSY mode.

void SetGranularity(int nGranularity = 5)

Sets the level of granularity used when in BPC_MODE_BUSY mode and using the BPC_BUSYFILL_SMOOTH fill option. The higher the granularity, the smoother the fill will appear, but it will also take longer to get from one side of the control to the other

int GetGranularity()

Gets the level of granularity used when in BPC_MODE_BUSY mode and using BPC_BUSYFILL_SMOOTH fill option.

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()

BPM_SETBUSYTYPE

Busy Mode Type (int)

N/A

SetBusyType(int nType)

BPM_SETBUSYFILL

Fill Type (int)

N/A

SetBusyFill(int nFill)

BPM_SETGRANULARITY

Granularity (int)

N/A

SetGranularity(int nGran)

BPM_SETCOLBKG

Colour (COLORREF)

N/A

SetColBkg(COLORREF col)

BPM_SETCOLBFACE

Colour (COLORREF)

N/A

SetColBlockFace(COLORREF col)

BPM_SETCOLBEDGE

Colour (COLORREF)

N/A

SetColBlockEdge(COLORREF col)

BPM_SETCOLBFACEHI

Colour (COLORREF)

N/A

SetColBlockFaceHi(COLORREF col)

BPM_SETCOLBEDGEHI

Colour (COLORREF)

N/A

SetColBlockEdgeHi(COLORREF col)

BPM_RECALC

N/A

N/A

Recalc()

BPM_RESET

N/A

N/A

Reset()

BPM_USESYSCOL

Flag (0 = false, non zero = true)

N/A

UseSysCol()

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

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

    History

    Version

    Release Date

    Change Log

    0.32003-05-05
  • Added BPM_XXXXX messages for the outstanding "SetXXX()" functions. The message list above has been updated
  • Fixed an acute threading bug (thanks to luedi for this one)
  • 0.212003-04-30
  • Fixed the threading bug which occurred when you rapidly called Start() in succession. Thanks to Mark Richards for letting me know
  • 0.202003-04-22
  • Added support for WM_SYSCOLORCHANGE message
  • Added flag for whether background colour was a system colour (true by default)
  • Changed all ON_MESSAGE() handlers to return an LRESULT (compatibility issue with VC7)
  • Added "Busy Type" flag to support different motion while in busy mode. Supported modes are: BPC_BUSY_PINGPONG, BPC_BUSY_LTR and BPC_BUSY_RTL
  • Added "Busy Fill" flag to support different filling methods while in busy mode. Supported fill modes are: BPC_BUSYFILL_BLOCK and BPC_BUSYFILL_SMOOTH
  • Updated source and demo .zip files
  • Fixed small bug in percentage calculation of the progress bar.
  • 0.12003-04-21
  • First public release
  • License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    Written By
    Architect
    Ireland Ireland
    Peter Mares has no comment on himself. I'll let the objective humanoids do the damage Wink | ;)
    He is currently developing a hobby MMO to prove that he can do it Wink | ;)

    My Blog

    All good things were meant to be improved

    Comments and Discussions

     
    GeneralRace Condition Problem leads to window handle problems / window flicker Pin
    Forest-Andi19-Mar-09 23:37
    Forest-Andi19-Mar-09 23:37 
    GeneralRe: Race Condition Problem leads to window handle problems / window flicker Pin
    Peter Mares24-Sep-10 19:23
    Peter Mares24-Sep-10 19:23 
    GeneralRe: Race Condition Problem leads to window handle problems / window flicker Pin
    Lampengeist26-May-11 23:55
    professionalLampengeist26-May-11 23:55 
    GeneralControl crashes when WaitForSingleObject returns nonsignaled Pin
    Frank1122334414-Feb-07 5:44
    Frank1122334414-Feb-07 5:44 
    GeneralRe: Control crashes when WaitForSingleObject returns nonsignaled Pin
    peter_cheng_test7-Jul-09 0:11
    peter_cheng_test7-Jul-09 0:11 
    GeneralThank you Pin
    Furno Gianluca2-Feb-06 22:36
    Furno Gianluca2-Feb-06 22:36 
    QuestionHow to update busy progress bar in modeless dialog Pin
    rnl@jk8-Jan-06 22:01
    rnl@jk8-Jan-06 22:01 
    AnswerRe: How to update busy progress bar in modeless dialog Pin
    Konstantin Malanin29-Jan-07 12:02
    Konstantin Malanin29-Jan-07 12:02 
    GeneralSir! can you help me! Pin
    ilansue26-Oct-05 0:51
    ilansue26-Oct-05 0:51 
    GeneralTransparent Background Pin
    wlburgess15-Oct-05 18:14
    wlburgess15-Oct-05 18:14 
    GeneralProgress in a loop Pin
    centuriansix5-Jun-05 20:22
    centuriansix5-Jun-05 20:22 
    GeneralRe: Progress in a loop Pin
    David Crow22-May-06 8:00
    David Crow22-May-06 8:00 
    GeneralResizable progress bar Pin
    Samuele23-Mar-05 4:33
    Samuele23-Mar-05 4:33 
    GeneralRe: Resizable progress bar Pin
    Catrick9-Jun-05 8:04
    Catrick9-Jun-05 8:04 
    GeneralBusy bar won't terminate Pin
    Cbenney0119-Aug-04 5:16
    Cbenney0119-Aug-04 5:16 
    GeneralPossibly Dumb Question Pin
    otrcomm11-Jul-04 12:18
    otrcomm11-Jul-04 12:18 
    GeneralRe: Possibly Dumb Question Pin
    otrcomm11-Jul-04 14:50
    otrcomm11-Jul-04 14:50 
    GeneralRe: Possibly Dumb Question Pin
    Peter Mares12-Jul-04 8:34
    Peter Mares12-Jul-04 8:34 
    GeneralRe: Possibly Dumb Question Pin
    otrcomm13-Jul-04 6:53
    otrcomm13-Jul-04 6:53 
    GeneralGets my 5! Pin
    RobJones14-May-04 11:00
    RobJones14-May-04 11:00 
    GeneralVery Impressive... Pin
    Doug Knudson3-Oct-03 10:16
    Doug Knudson3-Oct-03 10:16 
    GeneralRe: Very Impressive... Pin
    Guy Jackson9-Dec-03 3:14
    Guy Jackson9-Dec-03 3:14 
    GeneralVertical progress bar Pin
    Iv11-Jun-03 5:06
    Iv11-Jun-03 5:06 
    GeneralRational Purify error Pin
    Shielsy104-Jun-03 0:33
    Shielsy104-Jun-03 0:33 
    GeneralRunning this outside of my app Pin
    Tom Wright3-Jun-03 11:51
    Tom Wright3-Jun-03 11:51 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.