Siva Siva - 1 month ago 13
C++ Question

How to change the progress bar color?

I have a problem with changing the color of the progress bar. Initially I created an instance for that progress control and in the

OnInitDialog()
. I am trying change the color of the progress bar (initially the progress bar color is green). Now, I tried setting the bar color to red using this piece of code in the
OnInitDialog()
as follows,

BOOL OnInitDialog()
{
CPropertyPage::OnInitDialog();
m_ProgressBar->SetRange32(0,100);
m_ProgressBar->SetPos(50);
m_ProgressBar->SetBarColor(RGB(255,0,0));
return TRUE;
}


This is the thing I had to change the bar color, and I am using Windows 7 OS. One thing what I observed is when I changed the theme to "windows classic" then I am able to see the color I have set (in this case red). But again if I get back to the Windows 7 aero theme the color is green again.

Moreover I even checked the
SetBkColor
method here and it has the same issue that I faced when I used
SetBarColor()
.

Can anyone suggest a possible way to change the bar color in all themes (I think that would be fine if it supported all kinds of themes)?

Answer

The progress bar color is determined by the current system theme; there is no provided customization capability. If the user has the "Windows Classic" theme set, the progress bar fills with the system highlight color. If the user has the Aero theme set, then the Aero progress bar styles are used, which does indeed with green.

If you want to change how progress bars are rendered, you will need to change the system-wide theme. Of course, this is not something an application should do—it is something that the user would do because they prefer a different theme. For example, they might install a theme that has blue progress bars. There are plenty of examples online. It is easy to generate these themes; you simply open the aero.msstyle file in a resource editor and modify the images it uses to display progress bars.

However, the Aero-style progress bar does have three different states: normal, paused, and error. In the normal state, it is filled with green. In the paused state, it is filled with yellow, and in the error state, it is filled with red.

sample of Aero-style progress bars, each set to a different state

But you shouldn't change the state of the progress bar just because you want it to be a certain color—the three states have specific semantic meanings aside from their colors. In theory, the colors could even be changed to fit different locales (although I doubt they are). Consider what the Windows User Experience Guidelines have to say on progress bars; specifically:

Progress bar colors

  • Use red or yellow progress bars only to indicate the progress status, not the final results of a task. A red or yellow progress bar indicates that users need to take some action to complete the task. If the condition isn't recoverable, leave the progress bar green and display an error message.
  • Turn the progress bar red when there is a user recoverable condition that prevents making further progress. Display a message to explain the problem and recommend a solution.
  • Turn the progress bar yellow to indicate either that the user has paused the task or that there is a condition that is impeding progress but progress is still taking place (as, for example, with poor network connectivity). If the user has paused, change the Pause button label to Resume. If progress is impeded, display a message to explain the problem and recommend a solution.

If you've decided that changing the state of the progress bar is appropriate for your situation, you can do it by sending the the PBM_SETSTATE message to the control window:

SendMessage(hwndProgressBar, PBM_SETSTATE, PBST_ERROR, 0);

Of course, it only works when the Aero theme is enabled. Nothing will happen to the "Classic"-style progress bars.