C# 如何在进度栏中显示绿色进度以及其中的百分比?

C# 如何在进度栏中显示绿色进度以及其中的百分比?,c#,.net,winforms,C#,.net,Winforms,我有以下代码: private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { this.toolStripProgressBar2.Value = Math.Min(this.toolStripProgressBar2.Maximum, e.ProgressPercentage); this.toolStripProgressBar2.ProgressBa

我有以下代码:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
     this.toolStripProgressBar2.Value = Math.Min(this.toolStripProgressBar2.Maximum, e.ProgressPercentage);
     this.toolStripProgressBar2.ProgressBar.CreateGraphics().DrawString(e.ProgressPercentage.ToString() + "%", new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(this.toolStripProgressBar2.Width / 2 - 10, this.toolStripProgressBar2.Height / 2 - 7));
}
如果我只使用第二行,它将显示百分比,但没有绿色进度。 如果我也在使用第一行,则每次绿色进展时,百分比都将被删除

如果我只使用第二行,百分比看起来像是在自己身上画了一遍又一遍:

如果我只使用第一行,我将只看到绿色条进展良好,但没有百分比

这就是我现在尝试的:

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    using (Graphics gr = this.toolStripProgressBar2.ProgressBar.CreateGraphics())
    {
        gr.DrawString(e.ProgressPercentage.ToString() + "%",
            SystemFonts.DefaultFont,
            Brushes.Black,
            new PointF(this.toolStripProgressBar2.ProgressBar.Width / 2 - (gr.MeasureString(e.ProgressPercentage.ToString() + "%",
                SystemFonts.DefaultFont).Width / 2.0F),
            this.toolStripProgressBar2.ProgressBar.Height / 2 - (gr.MeasureString(e.ProgressPercentage.ToString() + "%",
                SystemFonts.DefaultFont).Height / 2.0F)));
    }
}
而progressBar中的百分比仍在不断地自我绘制

我正在使用包含
progressBar

gr.DrawString(percent.ToString() + "%",SystemFonts.DefaultFont,Brushes.Black,...
请查看下面的链接。你需要使用拖绳的过载来添加画笔


听起来一个UI线程正在阻止另一个。我也建议你检查一下。等等

它强制progressbar更新每个实例的进度

toolStripProgressBar2.Value = Math.Min(this.toolStripProgressBar2.Maximum,e.ProgressPercentage);


toolStripProgressBar2.ProgressBar.CreateGraphics().DrawString(e.ProgressPercentage.ToString() + "%", new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(this.toolStripProgressBar2.Width / 2 - 10, this.toolStripProgressBar2.Height / 2 - 7));
toolStripProgressBar2.PerformStep();

这是非常非常错误的代码。你应该处理你自己创建的
图形
实例。用我在此处链接中尝试使用的exmaple代码更新了我的问题。但是百分比还是一次又一次地被涂上油漆。奇怪。
toolStripProgressBar2.Value = Math.Min(this.toolStripProgressBar2.Maximum,e.ProgressPercentage);


toolStripProgressBar2.ProgressBar.CreateGraphics().DrawString(e.ProgressPercentage.ToString() + "%", new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(this.toolStripProgressBar2.Width / 2 - 10, this.toolStripProgressBar2.Height / 2 - 7));
toolStripProgressBar2.PerformStep();