2

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?

Hunter Mitchell
  • 7,063
  • 18
  • 69
  • 116
  • 1
    Check if setting `DoubleBuffered` to true makes any difference. – SimpleVar May 17 '12 at 20:42
  • where is DoubleBuffered? Is it in the progressbar...or what? I am new, so...sorry – Hunter Mitchell May 17 '12 at 20:44
  • @EliteGamer It is a property of the form itself. – SimpleVar May 17 '12 at 20:45
  • Also, why is my form closing so early...it gets to about 97 then stops – Hunter Mitchell May 17 '12 at 20:47
  • I think it is more logical to draw something in the paint method, than doing it from somewhere else. Performance wise I don't know if there's any different. DoubleBuffered can be of great help. – RvdK May 17 '12 at 20:50
  • Never use this method to draw on the controls. Every time the control redraw him self(invalidate), he will override your drawing which gives that flickering effect (you draw, he invalidate and erase and so on...). This has nothing to do with Double Buffering which also causes flickering effect but is more due to the lack of fps – Samy Arous May 17 '12 at 20:50
  • Check the code I uploaded, it should work as you expect it – Samy Arous May 17 '12 at 21:59

2 Answers2

7

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);
        }
    }
}
SimpleVar
  • 14,044
  • 4
  • 38
  • 60
Samy Arous
  • 6,794
  • 13
  • 20
  • Elite Gamer: Create an event handler for the control's Paint event. In the event handler body, use the Graphics object from PaintEventArgs parameter to do your drawing. Note that the Paint event may be raised many times, even between two ticks of your timer. – Libor May 17 '12 at 20:57
  • I do not see the paint thing for the progressbar? – Hunter Mitchell May 17 '12 at 20:58
  • After verification it seems option 2 is not possible for the particular case of a progress bar. The Paint event is hidden. I would go with option 1. – Samy Arous May 17 '12 at 21:00
  • check this for option 3: http://stackoverflow.com/questions/1517179/c-overriding-onpaint-on-progressbar-not-working – Samy Arous May 17 '12 at 21:01
  • but on number 1, the label will not be transparent...am i not correct? – Hunter Mitchell May 17 '12 at 21:05
  • 1
    @lcfseth Edited to make code more readable and elegant (with no annoying scroll bar) and fixed typo "pain message" to "paint message" – SimpleVar May 17 '12 at 22:00
1

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

Libor
  • 3,285
  • 1
  • 33
  • 41