ProgressBar Paint Method?

﹥>﹥吖頭↗ 提交于 2019-12-11 01:18:49

问题


OK, here is my code for what to paint on the progressbar:

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

Ok, so i am painting a label in the middle of it that will display the progressbar's value. For some reason, it keeps blinking....disappearing and reappearing. So, someone told me to take out that code and put it in the paint method.....i do not see it. Is there an easier way?


回答1:


Here's a code that should work (I went with option number 3, creating a child Class and overriding the WndProc to handle the paint message:

public class Prog : ProgressBar
{
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);

        if (m.Msg == 0x000F)
        {
            var flags = TextFormatFlags.VerticalCenter |
                        TextFormatFlags.HorizontalCenter |
                        TextFormatFlags.SingleLine |
                        TextFormatFlags.WordEllipsis;

            TextRenderer.DrawText(CreateGraphics(),
                                  ((float)this.Value/this.Maximum*100) + "%",
                                  Font,
                                  new Rectangle(0, 0, this.Width, this.Height),
                                  Color.Black,
                                  flags);
        }
    }
}



回答2:


You can also use ProgressBarRenderer to do the whole drawing by yourself.



来源:https://stackoverflow.com/questions/10643158/progressbar-paint-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!