Click here to Skip to main content
15,861,172 members
Articles / Desktop Programming / MFC

A Class to Combine Slider Control and Progress Bar Windows

Rate me:
Please Sign up or sign in to vote.
4.89/5 (31 votes)
23 Jan 2018CPOL2 min read 139K   7.2K   93   18
An article on how to combine Slider Controls and Progress bar into one class using custom draw

ProSlider Control - ProSlider.gif

Introduction

This article describes the implementation of CProSliderCtrl class, which uses custom draw functionality of CSliderCtrl class to combine Slider Control and Progress bar in one window.

This kind of a control is especially required for streaming multimedia applications, because of its less window space requirements and its freezing capability also helps in preventing unbuffered scenes to be viewed/listened.

Versions

  • 1.0 First release in 2004
  • 1.1 Updated the project to work with recent Visual Studio 2017

Features

CSliderCtrl has the following features:

  • All the basic features of the standard CSliderCtrl and CProgressCtrl classes
  • Custom draw borders, thumb, channel and progress bar
  • Borders could be shown or hidden
  • The colors could be changed on demand
  • The slider thumb could be disabled (frozen)
  • Flicker free progress bar

Background

The custom draw system heavily uses recursive functions to draw the windows' objects and uses negative exponential random numbers in order to get better colors (will be described later).

A recursive function calls itself for many times, until a control mechanism stops recursion. The prototype of this kind of function is given below:

C++
void Recursion(int val)
{
    // computing stuff
    val ++;
    if(val<=255)Recursion(val);
    return;
}

This is the easiest way to calculate a given color's lighter versions and also gives 3D look to the objects. You may examine the differences by commenting the recursion line inside the function in the given source code.

Since this recursive drawing function lightens the colors every time it calls itself, giving darker color values will result in better colored 3D objects. In order to generate dark colors to use in this recursive function, a negative exponential random number (nexp) generator is implemented. The simulation of the function in Matlab for the input 255 is given in the figure below:

Negative exponential of a given number

How to Use CProSliderCtrl Class

The exported functions are:

C++
// Gets the current lower and upper limits, or range, of the progress bar
// control.
void _GetRange(int& nLower, int& nUpper);

// Sets the upper and lower limits of the progress bar control's range and
// redraws the bar to reflect the new ranges.
void _SetRange(short nLower, short nUpper);

// Sets the upper and lower limits of the progress bar control's range and
// redraws the bar to reflect the new ranges.
void _SetRange32(int nLower, int nUpper);

// Sets the background color for the progress bar.
COLORREF _SetBkColor(COLORREF clrNew);

// Sets the thumb color
COLORREF _SetThumbColor(COLORREF clrNew);

// Sets the channel color
COLORREF _SetChColor(COLORREF clrNew);

// Gets the current position of the progress bar.
int _GetPos(void);

// Advances the current position of a progress bar control by a specified
// increment and redraws the bar to reflect the new position.
int _OffsetPos(int nPos);

// Sets the current position for a progress bar control and redraws the
// bar to reflect the new position.
int _SetPos(int nPos);

// Specifies the step increment for a progress bar control.
int _SetStep(int nStep);

// Advances the current position for a progress bar control by the step
// increment and redraws the bar to reflect the new position.
int _StepIt(void);

// De/Freezes the slider and returns the prev. state
BOOL Freeze(void);

// Enables/Disables borders
HRESULT _EnableBorders(BOOL bEnable=TRUE);

// Test if the borders are enabled or not
BOOL _IsEnabled(void);

CProSliderCtrl Class

This class uses the function OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult) to handle the custom drawing tasks. When the system sends TBCD_CHANNEL message, the class draws the progress bar to the area of the slider channel.

How to Use CProSliderCtrl Class in Your Applications

Simply add the class to your workspace and define the slider control variable as:

C++
// Pro Slider handle
CProSliderCtrl m_ProSlider;

and use the functions listed above to control progress bar, the functions starting with a "_" are related to progress bar controls.

Terms of Use

This code is free to use in any kind of application at your own risk.

License

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


Written By
Founder VeriTeknik B.V.
Netherlands Netherlands
Electrical & Electronic Engineer Ms.
Finished studying MS in Electrical & Electronics Eng. (2004).
Founder & Manager of VeriTech B.V. VeriTech.net

Comments and Discussions

 
QuestionThought Pin
Nelek23-Jan-18 22:26
protectorNelek23-Jan-18 22:26 
AnswerRe: Thought Pin
Cem KARACA24-Jan-18 10:38
professionalCem KARACA24-Jan-18 10:38 
QuestionVery nice components! Pin
kamiKamiKami00022226-May-15 23:59
kamiKamiKami00022226-May-15 23:59 
AnswerRe: Very nice components! Pin
Cem KARACA6-Aug-15 2:47
professionalCem KARACA6-Aug-15 2:47 
Generaldynamically created sliders Pin
Anonymous2-Jun-05 5:10
Anonymous2-Jun-05 5:10 
Generalvertical progress from bottom to top Pin
romansm18-Jan-05 0:08
romansm18-Jan-05 0:08 
GeneralRe: vertical progress from bottom to top Pin
Cem KARACA18-Jan-05 22:22
professionalCem KARACA18-Jan-05 22:22 
GeneralProgress bar drawing bug Pin
ericericeric8822553-Nov-04 7:15
ericericeric8822553-Nov-04 7:15 
Generalexcellent Pin
hozgur13-Jan-04 8:24
hozgur13-Jan-04 8:24 
General$25.00 Pin
PJ Arends8-Jan-04 12:38
professionalPJ Arends8-Jan-04 12:38 
GeneralRe: $25.00 Pin
Cem KARACA8-Jan-04 20:34
professionalCem KARACA8-Jan-04 20:34 
GeneralProgress bar messages Pin
PJ Arends8-Jan-04 12:30
professionalPJ Arends8-Jan-04 12:30 
GeneralRe: Progress bar messages Pin
Cem KARACA8-Jan-04 20:20
professionalCem KARACA8-Jan-04 20:20 
Very good idea!

I'll make the changes in the next release!

Thank you.

Cem KARACA
GeneralRe: Progress bar messages Pin
Andrew Smith12-Jan-04 14:32
Andrew Smith12-Jan-04 14:32 
GeneralVery nice control ;) Pin
Kochise8-Jan-04 8:38
Kochise8-Jan-04 8:38 
GeneralRe: Very nice control ;) Pin
Cem KARACA8-Jan-04 9:59
professionalCem KARACA8-Jan-04 9:59 
GeneralOne idea Pin
Dominik Reichl8-Jan-04 7:44
Dominik Reichl8-Jan-04 7:44 
GeneralRe: One idea Pin
Cem KARACA8-Jan-04 10:16
professionalCem KARACA8-Jan-04 10:16 

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.