表单加载时C#progressbar抽象字符串

时间:2013-04-18 10:08:01

标签: c# progress-bar default drawstring form-load

有没有办法在C#中的进度条上写一个默认文本?

这在Form_Load中不起作用,但在单击按钮时工作正常......

using (Graphics gr = progressBar1.CreateGraphics())
{
    StringFormat sf = new StringFormat(StringFormatFlags.NoWrap);
    sf.Alignment = StringAlignment.Center;
    gr.DrawString("hello world", 
                  new Font("Arial", 10.0f, FontStyle.Regular),
                  new SolidBrush(Color.Black), 
                  progressBar1.ClientRectangle, 
                  sf);                
}

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以设置一次性Timer事件作为一个选项执行此操作。

Timer t = new Timer();

Form_Load()
{
    t.Interval = 1000;
    t.Start();
}

static void t_Elapsed(object sender, ElapsedEventArgs e)
{
    //One time event
    t.Stop();
    using (Graphics gr = progressBar1.CreateGraphics())
    {
        StringFormat sf = new StringFormat(StringFormatFlags.NoWrap);
        sf.Alignment = StringAlignment.Center;
        gr.DrawString("hello world", 
            new Font("Arial", 10.0f, FontStyle.Regular),
            new SolidBrush(Color.Black), 
            progressBar1.ClientRectangle, 
            sf);                
    }
}

关于此的一点说明:

ProgressBar.Value改变了; ProgressBar图形将更改并覆盖您绘制的文本。 Nolonar用Label覆盖进度条并将其设置在ProgressBar前面的评论可能更符合您的需求。

否则,当图形发生变化时,您必须处理ProgressBar的重绘。