问题
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