ProgressBar绘制方法? [英] ProgressBar Paint Method?

查看:165
本文介绍了ProgressBar绘制方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这是我为了什么要在进度油漆代码:

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();
        }



好了,我画在它的中间,将显示一个标签进度条的值。出于某种原因,它不断闪烁....消失和再现。于是,有人告诉我拿出代码,并把它在paint方法.....我没有看到它。是否有更简单的方法?

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?

推荐答案

下面是一个代码,就可以(我选3号去,创建子类和重写的WndProc来处理绘制消息:

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);
        }
    }
}

这篇关于ProgressBar绘制方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆