自定义ProgressBar

大佬拿来的代码,根据需求修修改改。单纯记录一下,以后应该可以用到。

using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace CustomProgressBar
{
///
/// 自定义进度条
///
public class CustomProgressBarGif : Control
{
Graphics Graphics;
///
/// 缓冲
///
BufferedGraphics Buffer;

    /// <summary>
    /// 进度背景色1
    /// </summary>
    Color progressColor1;

    [Browsable(true)]
    public Color ProgressColor1
    {
        get { return progressColor1; }
        set { progressColor1 = value; }
    }
    /// <summary>
    /// 进度背景色2
    /// </summary>
    Color progressColor2;

    [Browsable(true)]
    public Color ProgressColor2
    {
        get { return progressColor2; }
        set { progressColor2 = value; }
    }
    /// <summary>
    /// 当前进度
    /// </summary>
    private int value;

    [Browsable(true)]
    public int Value
    {
        get { return this.value; }
        set
        {
            this.value = value;
            this.Refresh();
        }
    }

    /// <summary>
    /// 进度最大值
    /// </summary>
    private int maxValue;

    [Browsable(true)]
    public int MaxValue
    {
        get { return maxValue; }
        set { maxValue = value; }
    }

    /// <summary>
    /// 进度条X坐标
    /// </summary>
    public int gifX;
    [Browsable(true)]
    public int GifX
    {
        get { return gifX; }
        set { gifX = value; }
    }

    public CustomProgressBarGif()
    {
        value = 0;
        maxValue = 100;

        progressColor1 = Color.FromArgb(166, 72, 216);
        progressColor2 = Color.FromArgb(217, 184, 245);

        this.SetStyle(ControlStyles.UserPaint, true);
        this.SetStyle(ControlStyles.ResizeRedraw, true);
        this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

        InitGraphics();
    }

    ~CustomProgressBarGif()
    {
        if (Graphics != null)
        {
            using (Graphics)
            {
                Graphics = null;
            }
        }

        if (Buffer != null)
        {
            using (Buffer)
            {
                Buffer = null;
            }
        }
    }

    /// <summary>
    /// 初始化Graphics
    /// </summary>
    private void InitGraphics()
    {
        if (Buffer == null)
        {
            BufferedGraphicsContext context = BufferedGraphicsManager.Current;

            int width = 0;
            int height = 0;

            foreach (Screen item in Screen.AllScreens)
            {
                if (width < item.Bounds.Width)
                    width = item.Bounds.Width;
                if (height < item.Bounds.Height)
                    height = item.Bounds.Height;
            }

            Buffer = context.Allocate(this.CreateGraphics(), new Rectangle(0, 0, width, height));

            Graphics = Buffer.Graphics;
            Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            Graphics.SmoothingMode = SmoothingMode.HighQuality;
            Graphics.CompositingQuality = CompositingQuality.HighQuality;
            Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        }
    }
    /// <summary>
    /// 重写绘制函数
    /// </summary>
    /// <param name="e"></param>
    protected override void OnPaint(PaintEventArgs e)
    {
        if (this.Parent == null) return;

        //数字百分比
        string str = this.value.ToString() + "%";
        float X = Graphics.MeasureString(str, SystemFonts.MenuFont).Width + 220;
        float Y = Graphics.MeasureString(str, SystemFonts.MenuFont).Height / 100.0F;
        
        //清空上一次的绘制
        Graphics.Clear(this.Parent.BackColor);

        float height = e.ClipRectangle.Height;

        float width = e.ClipRectangle.Width - 3;

        //绘制背景的范围
        RectangleF rec = new RectangleF(0, 0, width, height);

        //圆角直径
        float radiu = this.Height;

        //绘制整个进度条背景
        using (GraphicsPath path = new GraphicsPath())
        {
            path.AddArc(rec.X, rec.Y, radiu, radiu, 90, 180);
            path.AddLine(rec.X + radiu, rec.Y, rec.X + rec.Width - radiu, rec.Y);
            path.AddArc(rec.X + rec.Width - radiu, rec.Y + rec.Height - radiu, radiu, radiu, 270, 180);
            path.AddLine(rec.X + rec.Width - radiu, rec.Y + rec.Height, rec.X + radiu, rec.Y + rec.Height);

            using (SolidBrush bru = new SolidBrush(this.BackColor))
            {
                Graphics.FillPath(bru, path);
            }
        }

        //计算当前进度长度
        float temp = (width / maxValue) * value;

        //进度条的当前进度给gif的X坐标
        gifX = (int)temp;

        if (temp > 0)
        {
            //绘制当前进度
            using (GraphicsPath path = new GraphicsPath())
            {
                rec = new RectangleF(0, 0, temp, height);

                path.AddArc(rec.X, rec.Y, radiu, radiu, 90, 180);
                path.AddLine(rec.X + radiu, rec.Y, rec.X + rec.Width - radiu, rec.Y);
                path.AddArc(rec.X + rec.Width - radiu, rec.Y + rec.Height - radiu, radiu, radiu, 270, 180);
                path.AddLine(rec.X + rec.Width - radiu, rec.Y + rec.Height, rec.X + radiu, rec.Y + rec.Height);

                using (LinearGradientBrush bru = new LinearGradientBrush(new PointF(0, 0), new PointF(10, 10), progressColor2, progressColor1))
                {
                    Graphics.FillPath(bru, path);
                }
            }
        }
        
        //有需要数字显示就取消注释
        //if(temp > 250)
        //    Graphics.DrawString(str, SystemFonts.MenuFont, Brushes.Black, new PointF(X, Y));
        //else
        //    Graphics.DrawString(str, SystemFonts.MenuFont, Brushes.White, new PointF(X, Y));

        Buffer.Render(e.Graphics);
    }
}

}

using System;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using ChenXi.QD2013;

namespace CustomProgressBar
{
public partial class ProgressBarEx : Form
{
CustomProgressBarGif progressBar;

    PictureBox picture;
    public ProgressBarEx()
    {
        InitializeComponent();
        //添加进度条
        LoadProderssBar();
        CheckForIllegalCrossThreadCalls = false;
        //Task.Factory.StartNew(() =>
        //{
        //    DoWork();
        //});

    }

    /// <summary>
    /// 添加进度条
    /// </summary>
    private void LoadProderssBar()
    {
        progressBar = new CustomProgressBarGif();
        progressBar.MaxValue = 15;
        progressBar.BackColor = Color.FromArgb(156, 68, 203);
        progressBar.Size = new Size(300, 15);
        progressBar.Location = new Point(70, 110);            

        picture = new PictureBox();
        picture.Location = new Point(30, 33);
        picture.Image = QDResource.初始化界面_进度条;
        picture.Width = 76;
        picture.Height = 74;

        this.Controls.Add(picture);
        this.Controls.Add(progressBar);
        
    }
    public void SetText(string text)
    {
        lb_curInfo.Text = text;
    }
    /// <summary>
    /// 更新进度
    /// </summary>
    public void DoWork()
    {
        progressBar.Value++;
        //Gif跟随进度条前进
        picture.Location = new Point(progressBar.gifX + 30, 33);
        //while (true)
        //{
        //    if (progressBar.IsHandleCreated && !progressBar.IsDisposed)
        //    {
        //        if (progressBar.Value >= progressBar.MaxValue)
        //        {
        //            break;
        //        }

        //        this.Invoke((Action)(() =>
        //        {
        //            progressBar.Value++;
        //            //Gif跟随进度条前进
        //            picture.Location = new Point(progressBar.gifX + 30, 33);
        //        }));

        //        Thread.Sleep(100);
        //    }
        //}
    }
}

}

这是已经用到项目中了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值