Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Changing ProgressBar Color of CProgressCtrl

1. CProgressCtrl & Progress Bar Color

A Progress Bar Control can be used to show the progress of a long running operation. In MFC, CProgressCtrl class is associated to the progress bar control. In this example, we will see how to set the progress bar background and the progress color. We can set the background color using the class member function. But, for setting the bar color, we need to send the message to progress bar from the containing parent.

This article applies to Visual Studio 2005 or earlier. If you use 2005 later versions, you can set the bar color using SetBarColor member function of the CProgressCtrl.

2. About the CProgressCtrl Example

Have a look at the below screenshot:

MFC legacy Progressbar Color Example
MFC legacy Progressbar Color Example

If you have default windows theme, the Start Progress button will start progress in a default Gray background and blue bar color. We simulate the bar animation with an induction of minor delay while progressing. The Reset Progress button will reset the progress to the start of the progress. The Set Red button of the sample changes the background color to white and foreground color to Red.

3. Add Control Variable for ProgressCtrl

First, we will create a dialog-based MFC application and name it as PrgrsClr. After we create the project, drag and drop a Progress Control from the toolbox to the dialog. In our example, the progress control ID is set to IDC_PROG_BAR. Similarly, add the other buttons as shown in the previous section. Handle the BN_CLICKED event for the buttons: Start Progress, Reset Progress, Set Red. The below screen shot shows handling the BN_CLICKED for the Start Progress button.

Button Click Handler
Button Click Handler

Next, we add a control variable for the Progress bar as shown in the below screenshot:

Adding CProgressCtrl Control variable
Adding CProgressCtrl Control variable
  1. Invoke Add Variable dialog from the Context menu by right clicking the class name from the class view tab (Marked as 1)
  2. In the Add Member variable dialog, select the progress bar control id (Marked as 2)
  3. Make sure the Category is set to Control (Marked as 3)
  4. Provide the control variable name as m_ctrl_progress (Marked as 4) and click the finish button (Marked as 5)

4. Setup MFC ProgressCtrl

First, declare a function in the header file, which we can use for initializing the progress bar. The code is below:

In the CPP file, the function InitProgress is implemented. The CProgressCtrl::SetRange function sets the progress bar range from number x -> y. In our case, we set the range for our progress control from 0 to 100. That means, when the progress control value is at 5, the bar appears at the initial stage and when the value is at 90, the bar approaches towards the end. In our example, each progress unit represents 1%. Next, we set the position at 0 using CProgressCtrl::SetPos. The progress control’s current position at zero shows that the progress is not yet started.

In the OnInitDialog of the class CPrgrsClrDlg, we make a call to the InitProgress so that when the dialog appears, the progress control initialized to 0-100 Range and control’s start position is at zero. Below is the code:

5.  Progress Increment of CProgressCtrl – Simulated

In the Start Progress button click handler, we increment the progress bar position one by one with a delay of 100 milliseconds. Here, we use a loop that runs for 100 times to increment the progress position. The Sleep function will set the delay of 100 milliseconds. Below is the code:

Next, we will make a call to the InitProgress in the Reset Progress button click handler. This will set the progress bar to its initial position. Below is the code:

6. Changing the progress bar color

 In the Set Red button click handler, the progress bar’s background color, as well as the Bar’s color, is changed. We set the background color white via the API call SetBkColor Whereas to set the progress bar color we make use of the SendMessage. In VS2005 and earlier versions, we can set the bar color by sending the  PBM_SETBARCOLOR message to the progress bar via the SendMessage. Below is the code:

Youtube: Watch the running Example here

Change Progressbar Color MFC 4

Source Code : Download

Categories: MFC

Tags: , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.