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:
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.
Next, we add a control variable for the Progress bar as shown in the below screenshot:
- Invoke Add Variable dialog from the Context menu by right clicking the class name from the class view tab (Marked as 1)
- In the Add Member variable dialog, select the progress bar control id (Marked as 2)
- Make sure the Category is set to Control (Marked as 3)
- 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:
1 2 |
//Sample 01: Initialize Progress Bar void InitProgress(); |
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.
1 2 3 4 5 6 |
//Sample 02: Initialize Progress Control void CPrgrsClrDlg::InitProgress() { m_ctrl_progress.SetRange(0,100); m_ctrl_progress.SetPos(0); } |
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:
1 2 3 4 5 6 7 |
// Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon //Sample 03: Make a call to Progress Control InitProgress(); |
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:
1 2 3 4 5 6 7 8 9 |
void CPrgrsClrDlg::OnBnClickedButtonStart() { //Sample 04: Start Progressing with a Delay for (int i = 0; i<=100; i++) { m_ctrl_progress.SetPos(i); Sleep(100); } } |
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:
1 2 3 4 5 |
void CPrgrsClrDlg::OnBnClickedButtonReset() { //Sample 05: Reset the Progress Status InitProgress(); } |
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:
1 2 3 4 5 6 |
void CPrgrsClrDlg::OnBnClickedButtonRed() { //Sample 06: Change Background to white and Bar color to Red m_ctrl_progress.SetBkColor(RGB(255, 255, 255)); m_ctrl_progress.SendMessage(PBM_SETBARCOLOR,0,RGB(255, 0, 0)); } |
Youtube: Watch the running Example here
Source Code : Download
Categories: MFC
Tags: CProgressCtrl::SetPos, CProgressCtrl::SetRange, PBM_SETBARCOLOR, SendMessage, SetBkColor