【发布时间】:2013-12-05 04:49:49
【问题描述】:
我创建了一个扩展 ProgressBar 的类 - 主要是为了允许更多的自定义,所以我可以在栏上绘制刻度,选择我想要的颜色,并摆脱默认的动画等等。它在大多数情况下都可以正常工作,但是第一次更改值时,在实际更新之前总是会有大约一秒钟的延迟。所有后续的更改都会立即发生,只是第一次从 0 变为 1 会延迟。我正在尝试确定是否存在某种我必须以某种方式规避的内置延迟?这是我的代码的相关位:
public class FancyProgressBar : ProgressBar
{
//... various properties and fields
public FancyProgressBar()
{
this.SetStyle(ControlStyles.UserPaint, true);
this.DoubleBuffered = true;
InitializeComponent();
}
public FancyProgressBar(IContainer container)
{
this.SetStyle(ControlStyles.UserPaint, true);
this.DoubleBuffered = true;
container.Add(this);
InitializeComponent();
}
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);
else
e.Graphics.DrawRectangle(Pens.Gray, 0, 0, this.Width, this.Height);
rec.Height = rec.Height - 4;
DrawBar(e.Graphics, new Color[] { Color1, Color2, Color3 }, e.ClipRectangle, rec);
if (Ticks > 0)
{
int lineHeight = this.Height * TickHeight / 100;
float spacing = this.Width / Ticks;
for (int i = 1; i < Ticks; i++)
{
Pen pen = new Pen(new SolidBrush(TickColor));
e.Graphics.DrawLine(pen, new Point((int)(spacing * i)-2, Height), new Point((int)(spacing * i)-2, Height - lineHeight));
}
}
}
//This is broken out into a separate function because I have another version
//that draws more than one bar. Shouldn't make a difference though.
private void DrawBar(Graphics g, Color[] colors, Rectangle clipRec, Rectangle drawRec)
{
using (System.Drawing.Drawing2D.LinearGradientBrush l =
new System.Drawing.Drawing2D.LinearGradientBrush(clipRec, Color.Green, Color.Red, 0f))
{
System.Drawing.Drawing2D.ColorBlend lb = new System.Drawing.Drawing2D.ColorBlend();
lb.Colors = colors;
lb.Positions = new float[] { 0, PositionColor2, 1.0f };
l.InterpolationColors = lb;
g.FillRectangle(l, 2, 2, drawRec.Width, drawRec.Height);
}
using (System.Drawing.Drawing2D.LinearGradientBrush l2 =
new System.Drawing.Drawing2D.LinearGradientBrush(clipRec,
Color.FromArgb(147, 255, 255, 255),
Color.FromArgb(0, 255, 255, 255),
System.Drawing.Drawing2D.LinearGradientMode.Vertical))
{
System.Drawing.Drawing2D.ColorBlend lb = new System.Drawing.Drawing2D.ColorBlend();
lb.Colors = new Color[] { Color.FromArgb(40, 255, 255, 255), Color.FromArgb(147, 255, 255, 255),
Color.FromArgb(40, 255, 255, 255), Color.FromArgb(0, 255, 255, 255) };
lb.Positions = new float[] { 0, 0.12f, 0.39f, 1.0f };
l2.InterpolationColors = lb;
l2.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile;
g.FillRectangle(l2, 2, 2, drawRec.Width, drawRec.Height);
}
}
编辑: 我应该声明我没有使用 ProgressBar 作为实际的进度条。它在加载内容时不会运行,只是在 UI 中显示一些值,因此不一定同时进行其他一些昂贵的计算。
【问题讨论】:
标签: c# winforms progress-bar