c#.网络进度条问题

本文关键字:问题 网络 | 更新日期: 2023-09-27 18:15:23

我正在从文件中读取数据,并希望将进度条附加到此操作。我在stackoverflow上找到了下面的代码——这段代码来自于William Daniel, 9月20日,stackoverflow上的一篇题为"如何在c#中改变进度条的颜色"的文章。Net 3.5 "

class CustomProgressBar : ProgressBar
{
    public CustomProgressBar()
    {
        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 = 2;
        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 rectangle
                rect.Width = (int)(rect.Width * ((double)this.Value / this.Maximum));
                if (rect.Width == 0) rect.Width = 1;
                LinearGradientBrush brush = new LinearGradientBrush(rect,
                      this.BackColor, this.ForeColor, LinearGradientMode.Vertical);
                offscreen.FillRectangle(brush, inset, inset, rect.Width, rect.Height);
                e.Graphics.DrawImage(offscreenImage, 0, 0);
                offscreenImage.Dispose();
            }
        }
    }
}

代码工作正常,除了以下内容:

  1. 渐变似乎没有扩展整个条的宽度。它在那里,但在条形图的底部更集中,当我们到达条形图的顶部时,它很快就变薄了。关于如何解决这个问题,有什么建议吗?

  2. 如果我将进度条放在窗体上,并在窗体上有进度条的部分上打开Internet Explorer窗口,则Internet窗口中的一些文本会溢出到窗体中的进度条上。我不知道为什么,以及如何解决这个问题。

作为题外话,如何估计您希望通过进度条显示的操作的长度?

任何帮助都将非常感激。谢谢你。

c#.网络进度条问题

对于问题的第一部分-尝试使用以下构造函数为您的画笔:

new LinearGradientBrush(new Point(0, 0), new Point(0, Height - inset * 2), BackColor, ForeColor);

您当前的画笔在Y=inset有顶部控制点-这就是为什么从Y=0Y=inset的区域都被漆成纯色。

您根本没有在进度条的非填充部分中绘图。尝试调用FillRectanglerect.Rightthis.Width的区域与背景颜色。它应该防止从重叠的窗口流出

试试这个,你不需要在每个油漆上创建一个图像…

 class CustomProgressBar : ProgressBar
 {
  public CustomProgressBar()
  {
   this.SetStyle(ControlStyles.UserPaint|ControlStyles.OptimizedDoubleBuffer|ControlStyles.DoubleBuffer, true);
   this.UpdateStyles();
  }
  protected override void OnPaint(PaintEventArgs e)
  {
   const int inset = 2;
   Rectangle rect = this.ClientRectangle;
   var offscreen = e.Graphics;
   if (ProgressBarRenderer.IsSupported){
    ProgressBarRenderer.DrawHorizontalBar(offscreen, rect);
   }
   rect.Inflate(new Size(-inset, -inset));  // Deflate inner rectangle
   rect.Width = Convert.ToInt32(Math.Round((rect.Width * ((double)this.Value / this.Maximum))));
   if (rect.Width == 0) rect.Width = 1;
   LinearGradientBrush brush = new LinearGradientBrush(rect,this.BackColor, this.ForeColor, LinearGradientMode.Vertical);
   offscreen.FillRectangle(brush, inset, inset, rect.Width, rect.Height);
   offscreen.DrawString(Value.ToString(), this.Font,Brushes.Black,rect);
  }
 }
使用

const int inset = 1;

另一个问题