我正在尝试将百分比添加到scrollBar,但为什么它会在进度时闪烁?

时间:2012-08-20 03:05:06

标签: c#

这是我正在使用的代码:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
            progressBar1.Value = e.ProgressPercentage;
            progressBar1.Refresh();
            int percent = (int)(((double)(progressBar1.Value - progressBar1.Minimum) /
(double)(progressBar1.Maximum - progressBar1.Minimum)) * 100);
            using (Graphics gr = progressBar1.CreateGraphics())
            {
                gr.DrawString(percent.ToString() + "%",
                    SystemFonts.DefaultFont,
                    Brushes.Black,
                    new PointF(progressBar1.Width / 2 - (gr.MeasureString(percent.ToString() + "%",
                        SystemFonts.DefaultFont).Width / 2.0F),
                    progressBar1.Height / 2 - (gr.MeasureString(percent.ToString() + "%",
                        SystemFonts.DefaultFont).Height / 2.0F)));
            }


            listBox1.Items.Add( "Converting File: " + e.UserState.ToString());
            textBox1.Text = e.ProgressPercentage.ToString();
}

在处理并向右移动时,百分比有些眨眼,但不够顺畅。

而且当它最终完成整个过程时,百分比消失了,只剩下绿色。

1 个答案:

答案 0 :(得分:0)

我刚试过你的代码而且我看到了你的问题,这是一个很好的问题。我研究过,发现你需要处理进度条的Paint事件并在e.Graphics上绘制。 否则,下次控件自行绘制时,您的绘图将被覆盖。

通常,您不应该使用CreateGraphics()。最好的方法是对进度条进行子类化。

首先需要做的是创建一个像这样的新类

class MyProgressBar : ProgressBar
{
    public MyProgressBar()
    {
        this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Rectangle rect = this.ClientRectangle;
        Graphics g = e.Graphics;

        ProgressBarRenderer.DrawHorizontalBar( g, rect );
        rect.Inflate(-3, -3);
        if ( this.Value > 0 )
        {
            Rectangle clip = new Rectangle( rect.X, rect.Y, ( int )Math.Round( ( ( float )this.Value / this.Maximum ) * rect.Width ), rect.Height );
            ProgressBarRenderer.DrawHorizontalChunks(g, clip);
        }

        // assumes this.Maximum == 100
        string text = this.Value.ToString( ) + '%';

        using ( Font f = new Font( FontFamily.GenericMonospace, 10 ) )
        {
            SizeF strLen = g.MeasureString( text, f );
            Point location = new Point( ( int )( ( rect.Width / 2 ) - ( strLen.Width / 2 ) ), ( int )( ( rect.Height / 2 ) - ( strLen.Height / 2 ) ) );
            g.DrawString( text, f, Brushes.Black, location ); 
        }
    }
}

然后转到下面的InitializeComponent方法并更新this.progressBar1 = System.Windows.Forms.ProgressBar();this.progressBar1 = new MyProgressBar();

#region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.progressBar1 = new MyProgressBar();

现在应该按照您的意愿工作。您的代码将缩减为此

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
            progressBar1.Value = e.ProgressPercentage;

            listBox1.Items.Add( "Converting File: " + e.UserState.ToString());
            textBox1.Text = e.ProgressPercentage.ToString();
}