简体   繁体   English

在ProgressBar上闪烁文本

[英]Blinking text on ProgressBar

I wrote a control. 我写了一个控件。 And when i execute program text on progressbar is blinking. 当我执行进度条上的程序文本时,会闪烁。

     public class ProgressBarPercentage : ProgressBar
        {
            private int percent;//user defined step

            public void SetProgressBarText(string text)
            {
                using (Graphics gr = this.CreateGraphics())
                {
                    this.Refresh();
                    gr.DrawString(percent.ToString() + " %", SystemFonts.DefaultFont, Brushes.Black,
                        new PointF(this.Width / 2 - (gr.MeasureString(percent.ToString() + " %", SystemFonts.DefaultFont).Width / 2.0F),
                        this.Height / 2 - (gr.MeasureString(percent.ToString() + " %", SystemFonts.DefaultFont).Height / 2.0F)));
                }
            }

            public void SetPercent(int percent)
            {
                this.percent = percent;
                if (this.Value < 100)
                    this.Value = percent;
                else
                    this.Value = 99;

            }
        }

static void Main
{
            ProgressBarWindow.ProgressBarPercentage progress = new ProgressBarWindow.ProgressBarPercentage();
    for (int i = 0; i < 100; i++)
    {     
        progress.SetPercent(i);
        progress.SetProgressBarText(i.ToString());
    }
}   

Can you say me what I did wrong? 你能说我做错了吗?

I do not think, You will see the blink effect by this, even if it works. 我认为,即使起作用,您也不会看到闪烁效果。

This is simply too fast. 这太快了。

You must perhaps use a timer to reduce the speed, or, also possible, System.Thread.Sleep() 您可能必须使用计时器来降低速度,或者也可能使用System.Thread.Sleep()

I know this question was asked long time ago, but maybe this answer would be helpful for someone with similar problem. 我知道这个问题是很久以前问过的,但是也许这个答案对有类似问题的人会有所帮助。 You can set style in constructor for this custom control like this: 您可以在构造函数中为此自定义控件设置样式,如下所示:

SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);

Most important is ControlStyles.OptimizedDoubleBuffer flag which can prevent controls from blinking when Paint is called in too short intervals. 最重要的是ControlStyles.OptimizedDoubleBuffer标志,它可以防止在间隔太短的时间内调用Paint时控件闪烁。

Source: http://msdn.microsoft.com/pl-pl/library/system.windows.forms.controlstyles.aspx 来源: http//msdn.microsoft.com/pl-pl/library/system.windows.forms.controlstyles.aspx

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

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