1

这是我正在使用的代码:

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();
}

虽然它正在处理并向右移动,但百分比是一些闪烁,它不够平滑。

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

4

1 回答 1

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();
}
于 2012-08-20T04:12:35.843 回答