简体   繁体   English

在进度栏中闪烁文本?

[英]Blinking text in progressbar?

i have made a cool progressbar with text in the middle of it. 我在其中的文本上做了一个很酷的进度栏。 Here is my code: 这是我的代码:

private void timer2_Tick(object sender, EventArgs e)
{
    progressBar1.Increment(+1);
    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)
        );
    if (progressBar1.Value >= 99)
    {
        timer2.Stop();
        this.Close();
    }
}

For some reason, the text shows up then disappears and other weird stuff. 由于某种原因,文本显示出来然后消失,并出现其他怪异的东西。 Why is that, and how do i fix it? 为什么会这样,我该如何解决?

尝试将绘图代码移到Paint事件中,基本上是在修改控件的视觉效果,因此您需要处理它而不是默认的绘画行为。

Your drawing gets erased whenever the control is repainted. 每当重画控件时,您的图形都会被删除。

Instead, you need to set a flag indicating whether to draw the text right now, then handle the Paint event, and, if the flag is true, draw the text. 相反,您需要设置一个标志,指示是否立即绘制文本,然后处理Paint事件,如果标志为true,则绘制文本。

In the timer tick handler, toggle the flag and call Invalidate() . 在计时器滴答处理程序中,切换标志并调用Invalidate()

另一种方法是创建一个UserControll,并在进度条顶部使用标签,然后分别使用每个标签,这样做可能会更容易实现,几乎不需要任何其他代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM