Click here to Skip to main content
15,867,453 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

     
    GeneralRe: thread problem Pin
    Peter Mares5-May-03 18:24
    Peter Mares5-May-03 18:24 
    GeneralRe: thread problem Pin
    Shielsy9893-Jun-03 23:28
    sussShielsy9893-Jun-03 23:28 
    GeneralRe: thread problem Pin
    Peter Mares3-Jun-03 23:39
    Peter Mares3-Jun-03 23:39 
    GeneralRe: thread problem Pin
    Shielsy104-Jun-03 0:28
    Shielsy104-Jun-03 0:28 
    Generala little bug Pin
    pepe_lon2-May-03 6:24
    pepe_lon2-May-03 6:24 
    GeneralRe: a little bug Pin
    Peter Mares4-May-03 19:50
    Peter Mares4-May-03 19:50 
    GeneralRe: a little bug Pin
    Michel Wassink5-May-03 10:14
    Michel Wassink5-May-03 10:14 
    GeneralRe: a little bug Pin
    pepe_lon6-May-03 19:43
    pepe_lon6-May-03 19:43 
    I'll send you the piece of code and I'll look if the problem is in my code or in the progress bar itself.This was the only working solution that I came upon.
    GeneralArticle and files updated again.. Pin
    Peter Mares2-May-03 3:48
    Peter Mares2-May-03 3:48 
    Generala naive question Pin
    Mister Transistor29-Apr-03 22:33
    Mister Transistor29-Apr-03 22:33 
    GeneralRe: a naive question Pin
    Peter Mares29-Apr-03 23:06
    Peter Mares29-Apr-03 23:06 
    GeneralVery nice, now expose it to .NET! Pin
    casperOne29-Apr-03 2:17
    casperOne29-Apr-03 2:17 
    GeneralRe: Very nice, now expose it to .NET! Pin
    Peter Mares29-Apr-03 2:49
    Peter Mares29-Apr-03 2:49 
    GeneralRe: Very nice, now expose it to .NET! Pin
    casperOne29-Apr-03 8:25
    casperOne29-Apr-03 8:25 
    GeneralRe: Very nice, now expose it to .NET! Pin
    Peter Mares29-Apr-03 19:22
    Peter Mares29-Apr-03 19:22 
    GeneralVery good - one problem Pin
    Mark Richards26-Apr-03 15:25
    Mark Richards26-Apr-03 15:25 
    GeneralRe: Very good - one problem Pin
    Peter Mares28-Apr-03 22:10
    Peter Mares28-Apr-03 22:10 
    GeneralHaving Trouble Pin
    kjessee26-Apr-03 6:10
    kjessee26-Apr-03 6:10 
    GeneralVery good Pin
    Brian Delahunty23-Apr-03 0:58
    Brian Delahunty23-Apr-03 0:58 
    GeneralArticle and files updated Pin
    Peter Mares22-Apr-03 9:52
    Peter Mares22-Apr-03 9:52 
    GeneralGreat - But a question Pin
    Anonymous22-Apr-03 7:02
    Anonymous22-Apr-03 7:02 
    GeneralRe: Great - But a question Pin
    Peter Mares22-Apr-03 7:48
    Peter Mares22-Apr-03 7:48 
    GeneralRe: Great - But a question Pin
    Anonymous22-Apr-03 8:11
    Anonymous22-Apr-03 8:11 
    GeneralProgress vs Busy Pin
    Rage22-Apr-03 4:52
    professionalRage22-Apr-03 4:52 
    GeneralRe: Progress vs Busy Pin
    Peter Mares22-Apr-03 9:50
    Peter Mares22-Apr-03 9:50 

    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.