I'd like to do two things on my progress bar.
- Change the green colour to red.
- Remove the blocks and make it in one color.
Any information about those two things I wonder how to accomplish will be greatfuly appreaciated!
Thanks.
I'd like to do two things on my progress bar.
Any information about those two things I wonder how to accomplish will be greatfuly appreaciated!
Thanks.
OK, it took me a while to read all the answers and links. Here's what I got out of them:
Sample Results
The accepted answer disables visual styles, it does allow you to set the color to anything you want, but the result looks plain:

Using the following method, you can get something like this instead:

How To
First, include this if you haven't: using System.Runtime.InteropServices;
Second, you can either create this new class, or put its code into an existing static non-generic class:
public static class ModifyProgressBarColor
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr w, IntPtr l);
public static void SetState(this ProgressBar pBar, int state)
{
SendMessage(pBar.Handle, 1040, (IntPtr)state, IntPtr.Zero);
}
}
Now, to use it, simply call:
progressBar1.SetState(2);
Note the second parameter in SetState, 1 = normal (green); 2 = error (red); 3 = warning (yellow).
Hope it helps!
Since the previous answers don't appear to work in with Visual Styles. You'll probably need to create your own class or extend the progress bar:
public class NewProgressBar : ProgressBar
{
public NewProgressBar()
{
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rec = e.ClipRectangle;
rec.Width = (int)(rec.Width * ((double)Value / Maximum)) - 4;
if(ProgressBarRenderer.IsSupported)
ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle);
rec.Height = rec.Height - 4;
e.Graphics.FillRectangle(Brushes.Red, 2, 2, rec.Width, rec.Height);
}
}
EDIT: Updated code to make the progress bar use the visual style for the background
In the designer, you just need to set the ForeColor property to whatever color you'd like. In the case of Red, there's a predefined color for it.
To do it in code (C#) do this:
pgs.ForeColor = Color.Red;
Edit: Oh yeah, also set the Style to continuous. In code, like this:
pgs.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
Another Edit: You'll also need to remove the line that reads Application.EnableVisualStyles() from your Program.cs (or similar). If you can't do this because you want the rest of the application to have visual styles, then I'd suggest painting the control yourself or moving on to WPF since this kind of thing is easy with WPF. You can find a tutorial on owner drawing a progress bar on codeplex
Usually the progress bar is either themed or honors the user's color preferences. So for changing the color you either need to turn off visual styles and set ForeColor or draw the control yourself.
As for the continuous style instead of blocks you can set the Style property:
pBar.Style = ProgressBarStyle.Continuous;
EDIT
By the sounds of things you're using the XP Theme which has the green block based prog-bar. Try flipping your UI Style to Windows Classic and test again, but you may need to implement your own OnPaint event to get it to do what you want across all UI Styles
Or as someone else pointed out, disable the VisualStyles for your application.
Original
As far as I know, the rendering of the Progress bar happens inline with the windows theme style that you've chosen (win2K, xp, vista)
You can change the color by setting the property
ProgressBar.ForeColor
I'm not sure that you can do much more however...
does some googling
Theres an article here from MS KB on creating a "Smooth" progress bar
try using messsage PBM_SETBARCOLOR, that should do the trick with SendMessage
See http://www.vbforums.com/showthread.php?t=248721 for an example.
Just in case anyone looks for another option.... you can extend a Panel, use it as background (white or whatever), add another Panel inside it for the foreground (the moving bar). Then you have total control of changing the color, etc.
I found this can be done by drawing a rectangle inside the progress bar, and to set its width according to the progress's current value. I also added support for right to left progress. This way you don't need to use the Image, and since Rectnalge.Inflate isn't called the drawn rectangle is smaller.
public partial class CFProgressBar : ProgressBar
{
public CFProgressBar()
{
InitializeComponent();
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaintBackground(PaintEventArgs pevent) { }
protected override void OnPaint(PaintEventArgs e)
{
double scaleFactor = (((double)Value - (double)Minimum) / ((double)Maximum - (double)Minimum));
int currentWidth = (int)((double)Width * scaleFactor);
Rectangle rect;
if (this.RightToLeftLayout)
{
int currentX = Width - currentWidth;
rect = new Rectangle(currentX, 0, this.Width, this.Height);
}
else
rect = new Rectangle(0, 0, currentWidth, this.Height);
if (rect.Width != 0)
{
SolidBrush sBrush = new SolidBrush(ForeColor);
e.Graphics.FillRectangle(sBrush, rect);
}
}
}
I think that the simplest solutions of all, it's just a quick fix, but you can delete, comment the Application.EnableVisualStyles() from Program.cs, or however you have name your the part containing the Main function. After that you can freely change the color form the progress bar by progressBar.ForeColor = Color.TheColorYouDesire;
static void Main()
{
//Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
Edit: in the two minuites it took me to fire up vs and check the syntax i was beaten to it with much better responses. i love this site.
progressBar1 = new ProgressBar();
progressBar1.ForeColor = Color.Red;