private void NewsUpdate()
{
counter += 1;
progressBar1.Value = counter;
int percent = progressBar1.Value;
progressBar1.CreateGraphics().DrawString(
percent.ToString() + "%", new Font("Arial", (float)8.25, FontStyle.Regular),
Brushes.Black, new PointF(progressBar1.Width / 2 - 10, progressBar1.Height / 2 - 7));
//progressBar1.Refresh();
label9.Text = counter.ToString();
label9.Visible = true;
if (counter == 10)
{
Extractions();
counter = 0;
}
}
这样我就可以看到progressBar1中间的百分比,但它会闪烁。 我试图添加progressBar1.Refresh();在绘图之前和之后尝试使progressBar1无效,但没有任何帮助它闪烁。
这个方法NewsUpdate我通过计时器滴答事件调用它,它的间隔设置为1000毫秒
如何在没有眨眼的情况下使其平滑?
修改
这就是我添加了一个OnPaint覆盖事件:
protected override void OnPaint(PaintEventArgs e)
{
int percent = progressBar1.Value = counter;
base.OnPaint(e);
e.Graphics.DrawString(percent.ToString() + "%",
SystemFonts.DefaultFont,
Brushes.Black,
new PointF(progressBar1.Width / 2 - (e.Graphics.MeasureString(percent.ToString() + "%",
SystemFonts.DefaultFont).Width / 2.0F),
progressBar1.Height / 2 - (e.Graphics.MeasureString(percent.ToString() + "%",
SystemFonts.DefaultFont).Height / 2.0F)));
}
在方法中我更新了我在无效时执行的计时器:
private void NewsUpdate()
{
counter += 1;
this.Invalidate();
现在的问题是我看到form1顶部的百分比。如何将百分比移动到中间的progressBar上?
修改
我现在尝试了这个:
protected override void OnPaint(PaintEventArgs e)
{
int percent = progressBar1.Value = counter;
base.OnPaint(e);
Graphics gr = e.Graphics;
using (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)));
}
}
现在它在progressBar上,但再次闪烁。百分比在闪烁。
答案 0 :(得分:0)
如果您已将进度条的最大值设置为100,则无需根据进度条值计算百分比。
private void progressbar1_Paint(object sender, PaintEventArgs e)
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Font font = new Font("Arial", 8.25f, FontStyle.Bold, GraphicsUnit.Point);
RectangleF rf = new RectangleF(0,0,progressbar1.Width, progressbar1.Height);
e.Graphics.DrawString(progressbar1.Value.ToString("# %"), font, Brushes.Black,rf, sf);
}
并将progressbar1.Invalidate()
放在要更新progressbar1.Value
progressbar1.Value += 1;
progressbar1.Invalidate();