Reputation: 709
I have been looking for this solution for a quite long time, here, or other guidance sites.. But many people want to just chhange the foreground color of progressbar (the part, which is rising with progress)
I think, my small image can say everything:
http://screenshot.cz/WT5KO/progbar.png
Now, I am using this code to paint custom bar, which you can see above. Is it possible compose the background-edit code into my constructor code?
public class NewProgressBar : ProgressBar
{
public NewProgressBar()
{
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
// None... Helps control the flicker.
}
protected override void OnPaint(PaintEventArgs e)
{
const int inset = 0; // A single inset value to control teh sizing of the inner rect.
using (Image offscreenImage = new Bitmap(this.Width, this.Height))
{
using (Graphics offscreen = Graphics.FromImage(offscreenImage))
{
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
if (ProgressBarRenderer.IsSupported)
ProgressBarRenderer.DrawHorizontalBar(offscreen, rect);
rect.Inflate(new Size(-inset, -inset)); // Deflate inner rect.
rect.Width = (int)(rect.Width * ((double)this.Value / this.Maximum));
if (rect.Width == 0) rect.Width = 1; // Can't draw rec with width of 0.
TextureBrush mybrush = new TextureBrush(Properties.Resources.loading);
LinearGradientBrush brush = new LinearGradientBrush(rect, Color.DarkGoldenrod, Color.Gold, LinearGradientMode.Vertical);
offscreen.FillRectangle(mybrush, inset, inset, rect.Width, rect.Height);
e.Graphics.DrawImage(offscreenImage, 0, 0);
offscreenImage.Dispose();
}
}
}
}
I use WFA template under .NET 4.5
thanks in advance
Upvotes: 1
Views: 240
Reputation: 9
You are using visual progress bar style on your custom progress bar because you declare your control as progress bar at your class.
public class NewProgressBar : ProgressBar
if you want to make your own please switch your custom control class declaration into Control
public class NewProgressBar : Control
then you will able to use BackColor but you need to code 'value, minimum/maximum value,' by your own to make that progress bar works
Upvotes: 1