Introduction
Originally developed based on the work of Chris Maunder on CProgressBar. This CSkinProgress class intend to be used everywhere some calculations are needed. It features :
- Bitmapped progress bar (default, from resource - which need little code modification - or from external source)
- Auto-sizing of the progress bar
- Auto-sizing of the text
- Paning of the progress bar
- Optional completion status message (percent or timed)
- And more...
All features can be changed during run-time :
- Switch in real-time between different bitmaps according to your mood or needs (normal mode, alert bar, ...)
- Change the text to show different states of the process being calculated
- Change the range of the progress bar with resample if more accuracy provided
- And more...
Now the explanation of the features...
Features
1. Mode
CSkinProgress
allows to create a progress bar in a status bar or in a dialog. All depends on the constructor you will use and which parameters you will provide to CSkinProgress
.
Status Bar
Collapse
|
Copy Code
CSkinProgress
(
LPCTSTR i_poStrMessage,
int i_nUpper = 100,
int i_nProgress = cSPT_PERCENT,
int i_nPane = 0,
int i_nSize = 200,
CBitmap* i_poBitmap = NULL
#ifdef dCSP_SLIDERBAR_METHOD
,BOOL i_bReverse = false
#endif );
With this constructor you can create a progress bar with text, completion status, select the pane, the bitmap and if the progress bar have to be displayed from the end.
Dialog
Collapse
|
Copy Code
#ifdef dCSP_DIALOG_PROGRESS
CSkinProgress
(
CWnd* i_poWndProgress,
int i_nUpper = 100,
CBitmap* i_poBitmap = NULL,
#ifdef dCSP_SLIDERBAR_METHOD
BOOL i_bReverse = false,
#endif CWnd* i_poWndMessage = NULL,
LPCTSTR i_poStrMessage = NULL,
int i_nProgress = cSPT_NONE
);
#endif
If the dCSP_SLIDERBAR_METHOD
define is activated, you can use this constructor to create a progress bar inanyCWnd
object with a parent (important) in the dialog. You don't have to provide a pane number, but a CWnd
anchor used to create the progress bar, and another CWnd
in which the text will be displayed.
2. Sizing
You can provide a size for the progress bar. This size is only used when the progress bar is located in the status bar and in pane 0, thus share it with the text. You can select 3 modes of text sharing :
Positive size
If the given size is positive, the size of the progress bar is FIXED
. Thus the text is wrapped if there is not enough place.
Zero size
If the given size is null, the size of the progress bar fits the remaining place left by the text. Thus the text is never wrapped if there is not enough place. It can only be if there is a completion message that takes too much place.
Negative size
If the given size is negative, the size of the progress bar fits the remaining place left by the text if there is no enough place, otherwise the given size is used.
3. Text
You can provide any text you want (Note: not too long - up to 128 characters).
4. The Panes
You can create as many panes as you want in the status bar, just follow these rules...
Add an ID in the string table, in the ID_INDICATORS
section, otherwise the text will appears in the pane. The longer the text, the more the width of the pane will be important !
Now add this user pane ID in the 'indicators' array in the beginning of the 'MainFrm.cpp' file. First entry is pane 0.
Then, select the pane number you will use to display the progress bar. For instance, the user pane is number 0 where the text is located :
You can even use system panes, such pane 3 where the NUM-LOCK indicator is located:
5. The completion message
You can ask to CSkinProgress
to add a message at the end of your text in order to provide the current completion status. The progress bar gives a graphical information, but sometimes it is useful to have more information. An enum
list is fitted with increasing value with a different meaning for each of them. Here are the different kinds of messages :
cSPT_NONE
: Don't add any messages, quite fast.
cSPT_PERCENT
: Add a simple text percent completion report, a bit slower than NONE
cSPT_TIMED
: Add a full features message that gives the date/time of start, the current percentage of completion, the remaining percentage, the remaining time, and the expected date/time of end - quite slow
cSPT_AUTOSIZE
: Automaticaly select the best message between PERCENT and TIMED according to the remaining place in pane 0 - the slowest.
NEGATIVE
: Suppress the parenthesis (NEW) You can now delete the parenthesis by providing a negative progress number, like you can stop the wrapping of the text by providing a negative size for the progress bar in pane 0 !
6. The bitmap
You can select the default and embedded bitmap (which is the same as IDB_BMP_AQUA
) or use a resource bitmap (needs to change conditions for the compilation and modify the code in CSkinProgress::SetBitmap(...)
)
You can also provide a bitmap to the CSkinProgress
7. Reverse display
When not selected, the progress bar start from the left (or top in a vertical progress bar).
For some reasons you can select to display the progress bar from its end. Especially useful for vertical bar in dialog, so that it starts from the bottom, like a thermometer.
The Code
Constructors
Status bar
Collapse
|
Copy Code
CSkinProgress::CSkinProgress
( )
Create a default progress bar in the status bar with "Work in progress" and the default constructor values given below.
Collapse
|
Copy Code
CSkinProgress::CSkinProgress
( LPCTSTR i_poStrMessage, int i_nUpper, int i_nProgress, int i_nPane, int i_nSize, CBitmap* i_poBitmap #ifdef dCSP_SLIDERBAR_METHOD
,BOOL i_bReverse #endif )
WARNING : This constructor has changed since the original version 1.00/1.12dev !
The most simple status bar constructor is :
Collapse
|
Copy Code
CSkinProgress::CSkinProgress
( LPCTSTR i_poStrMessage, )
Dialog
Collapse
|
Copy Code
#ifdef dCSP_DIALOG_PROGRESS
CSkinProgress::CSkinProgress
( CWnd* i_poWndProgress, int i_nUpper, CBitmap* i_poBitmap, #ifdef dCSP_SLIDERBAR_METHOD
BOOL i_bReverse, #endif CWnd* i_poWndMessage, LPCTSTR i_poStrMessage, int i_nProgress )
#endif
To use the dialog constructor, make sure the dCSP_DIALOG_PROGRESS
define is set. You can bypass the text pane declaration, as I have put the i_bReverse
parameter in the middle of the function parameter list. Beware of your coding, you may use also the dCSP_SLIDERBAR_METHOD
define yourself in order to provide conditionnal compilated facilities to your own code.
The most simple status bar constructor is :
Collapse
|
Copy Code
CSkinProgress::CSkinProgress
( CWnd* i_poWndProgress, )
Destructor
Collapse
|
Copy Code
CSkinProgress::~CSkinProgress
( )
Bitmap Process
Collapse
|
Copy Code
BOOL CSkinProgress::SetBitmap
( CBitmap* i_poBitmap #ifndef dCSP_TIMED_REDRAW
,BOOL i_bDisplay #endif )
The two following addi(c)tive functions are used by CSkinProgress::SetBitmap(...)
in order to copy/convert TRUE/COLOR bitmap pictures (don't support mapped color bitmap yet). They can be used for your own purposes :
Collapse
|
Copy Code
BOOL CSkinProgress::CopyBitmap
( CBitmap* o_poBitmap, CBitmap* i_poBitmap )
Collapse
|
Copy Code
BOOL CSkinProgress::ConvBitmap
( CBitmap* o_poBitmap, CBitmap* i_poBitmap, CDC* i_poDC )
Progress Interface
Collapse
|
Copy Code
BOOL CSkinProgress::SetRange
( int i_nLower, int i_nUpper, int i_nStep, BOOL i_bResamble #ifndef dCSP_TIMED_REDRAW
,BOOL i_bDisplay #endif )
Collapse
|
Copy Code
int CSkinProgress::SetPos
( int i_nPos #ifndef dCSP_TIMED_REDRAW
,BOOL i_bDisplay #endif )
Collapse
|
Copy Code
int CSkinProgress::OffsetPos
( int i_nOffset #ifndef dCSP_TIMED_REDRAW
,BOOL i_bDisplay #endif )
Collapse
|
Copy Code
int CSkinProgress::SetStep
( int i_nStep )
Collapse
|
Copy Code
int CSkinProgress::StepIt
( #ifndef dCSP_TIMED_REDRAW
BOOL i_bDisplay #endif )
Extended Progress Interface
Collapse
|
Copy Code
BOOL CSkinProgress::SetSize
( int i_nSize #ifndef dCSP_TIMED_REDRAW
,BOOL i_bDisplay #endif )
Collapse
|
Copy Code
int CSkinProgress::GetSize
( )
Collapse
|
Copy Code
int CSkinProgress::GetPos
( BOOL i_bPercent )
Collapse
|
Copy Code
int CSkinProgress::GetStep
( )
Collapse
|
Copy Code
int CSkinProgress::GetLower
( )
Collapse
|
Copy Code
int CSkinProgress::GetUpper
( )
Collapse
|
Copy Code
#ifdef dCSP_SLIDERBAR_METHOD
int CSkinProgress::SetStart
( int i_nStart #ifndef dCSP_TIMED_REDRAW
,BOOL i_bDisplay #endif )
#endif
Collapse
|
Copy Code
#ifdef dCSP_SLIDERBAR_METHOD
int CSkinProgress::GetStart
( BOOL i_bPercent )
#endif
Collapse
|
Copy Code
void CSkinProgress::Reset
( #ifndef dCSP_TIMED_REDRAW
BOOL i_bDisplay #endif )
Text Interface
Collapse
|
Copy Code
BOOL CSkinProgress::SetText
( LPCTSTR i_poStrMessage #ifndef dCSP_TIMED_REDRAW
,BOOL i_bDisplay #endif )
Collapse
|
Copy Code
BOOL CSkinProgress::SetProgress
( int i_nProgress #ifndef dCSP_TIMED_REDRAW
,BOOL i_bDisplay #endif )
Collapse
|
Copy Code
int CSkinProgress::GetProgress
( )
Update Process
Collapse
|
Copy Code
BOOL CSkinProgress::RefreshPanes
( )
Compiling the Source
Manage the CSkinProgress
features with conditionnal compilation ! Here the explanation of each compilation define :
dCSP_DIALOG_PROGRESS
: Activate the use of CSkinProgress
in dialogs
dCSP_TIMED_REDRAW
: When set/uncommented, don't redraw/refresh the text/progress bar at each event but only 50 times a second (enough for accuracy). In some loops, CSkinProgress::StepIt()
can be easily called far more than 50 times a second !
dCSP_VERTICAL_BAR
: Activate the full vertical bar calculation. Not used in the status bar, but can be used when dCSP_DIALOG_PROGRESS
is set
dCSP_SLIDERBAR_METHOD
: Turns the progress bar into a full slider bar. Allows the bar to be reverse displayed, so uncomment this define if you want to use the 'reverse' feature of CSkinProgress
Specific defines are used for very experimental purposes, use them with care :
dCSP_RESOURCE_BITMAP :
Use a resource bitmap as default bitmap. The current ID name of the resource bitmap is IDB_AQUABAR
, but you mey change this by modifying the name of the ressource bitmap inCSkinProgress::SetBitmap(...)
dCSP_CREATE_BITMAP_FILE :
At the end of CSkinProgress::SetBitmap(...)
, create a source file in the root of C: ! Used to change the default embedded bitmap : create a CSkinProgress
and provide a user bitmap, you'll get the converted bitmap into a source file !
dCSP_DISPLAY_STRETCH :
Display the stretched picture used for the image list creation. There is hardly usage of this feature, beside documentation or verification of the CSkinProgress::SetBitmap(...)
function
The Progress Bar Bitmap
Now to finish this little introduction to the CSkinProgress
graphic engine, the overview of the method used to create the progress bar picture. A bitmap is provided to CSkinProgress::SetBitmap(...)
...
This bitmap is divided into 9 parts...
From left to right, here are the meaning and associated ID define :
cSPB_START
: The start of the progress bar background
cSPB_BEFORE
: The element before the beginning of the bar, could be an hilight
cSPB_LEFT
: The left part of the bar, its beginning
cSPB_CENTER
: The center part of the progress bar, could be interpreted as a grab knot
cSPB_BAR
: The bar body
cSPB_RIGHT
: The right part of the bar, its end
cSPB_AFTER
: The element after of the end of the bar, could be a shadowing
cSPB_BACKGROUND
: The progress bar background
cSPB_END
: The end of the progress bar background
To create your own bitmap skin, make any bitmap with 9 parts like above. The height of the bitmap is not really important (BTW not too large - no more than 32 pixels, of course) and the width should be multiple of 9 (important), thus : width % 9 = 0 ! Have a look at the bonus/hidden bitmaps to understand that/howCSkinProgress
allows you to make funny things ;)
Relative positions of the keys elements are calculated on the range of the progress bar and its current position. Then, once get these values as percent, calculate the absolute positions of these key elements. There is now just the place them on the progress bar image in a defined order so that some elements could hide some others, like on layers in Paint Shop Pro.
Blue columns are the exact positions of the elements.
Red columns are the base columns based on the width of an element.
The three/four letters are the key of comments into the CSkinProgress::OnPaint()
function for you to spot them easily...
Version history
Rev 1.02 : July 12 2003 14:01:53 (Kochise)
Base : CSkin
Progress 1.32dev
-
extracting files : no error
parsing files : no error
diff engine : no error
modify.public.constructor:
CSkinProgress
touch.protected.function:
CreateCommon
modify.protected.function:
ProgressInStatusBar
modify.protected.function:
ProgressInDialog
modify.public.function:
SetRange
touch.public.function:
SetPos
modify.public.function:
GetPos
touch.public.function:
SetStart
modify.public.function:
GetStart
modify.public.function:
Reset
add.public.function:
SetProgress
add.public.function:
GetProgress
modify.public.function:
RefreshPanes
touch.protected.function:
CompactText
modify.protected.function:
GetTimed
modify.protected.function:
UpdateProgress
modify.protected.function:
OnPaint
compiling project files : no error
packing files : no error
Rev 1.01 : July 06 2003 22:08:37 (Kochise)
Base : CSkin
Progress 1.28 Devel
-
extracting files : no error
parsing files : no error
diff engine : no error
modify.public.constructor:
CSkinProgress
add.public.constructor:
CSkinProgress
modify.public.function:
SetBitmap
modify.public.function:
ConvBitmap
modify.public.function:
SetRange
modify.public.function:
SetPos
modify.public.function:
OffsetPos
modify.public.function:
StepIt
modify.public.function:
SetSize
add.public.function:
GetSize
add.public.function:
GetPos
add.public.function:
GetStep
add.public.function:
GetLower
add.public.function:
GetUpper
add.public.function:
SetStart
add.public.function:
GetStart
add.public.function:
Reset
modify.public.function:
SetText
mutate.function:
protected.ResizeTextPane->
public.RefreshPanes
modify.public.function:
RefreshPanes
mutate.function:
public.Clear->
protected.Clear
add.protected.function:
GetTargetRect
mutate.protected.function:
Create->
CreateCommon
modify.protected.function:
CreateCommon
add.protected.function:
ProgressInStatusBar
add.protected.function:
ProgressInDialog
modify.protected.function:
CompactText
add.protected.function:
UpdateProgress
modify.protected.function:
OnPaint
compiling project files : no error
packing files : no error
Rev 1.00 : May 25 2003 13:25:12 (Kochise)
First archived version and released onto CodeProject.
Base : CSkin
Progress 1.12 Devel
-
add.public.constructor:
CSkinProgress
add.public.constructor:
CSkinProgress
add.public.function:
SetRange
add.public.function:
SetText
add.public.function:
SetSize
add.public.function:
SetBitmap
add.public.function:
CopyBitmap
add.public.function:
ConvBitmap
add.public.function:
SetPos
add.public.function:
OffsetPos
add.public.function:
SetStep
add.public.function:
StepIt
add.public.function:
Clear
add.public.destructor:
~CSkinProgress
add.protected.function:
Create
add.protected.function:
GetStatusBar
add.protected.function:
ResizeTextPane
add.protected.function:
CompactText
add.protected.function:
GetTimed
add.protected.function:
OnEraseBkgnd
add.protected.function:
OnPaint
add.protected.function:
OnSizing
add.protected.function:
OnSize
add.protected.function:
OnTimer
compiling project files : no error
packing files : no error
转自:http://www.codeproject.com/Articles/4204/CSkinProgress