C#自定义进度条控件

自定义进度条类,Winform自定义控件,如果有兴趣,可以自己参考写一个WPF版的。当然,Winform的控件在WPF上也是可以使用的。

自定义用户控件

//CircularProgressBar.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Insulin.Client.Circle
{
    public class CircularProgressBar : Control
    {
        private Color mainColor = Color.LimeGreen;
        public string Strtxt = "";
        public int Num = 0;
        public Color MainColor
        {
            get { return mainColor; }
            set
            {
                mainColor = value;
                this.Invalidate();
            }
        }
        private double value = 20.00;

        public double Value
        {
            get { return this.value; }
            set
            {
                this.value = value;
                this.Invalidate();
            }
        }

        private int lastWidth = 0;
        private int lastHeight = 0;
        private Point lastLocation = Point.Empty;
        public CircularProgressBar(string strtxt,int num)
        {
            Strtxt = strtxt;
            Num = num;
            this.SetStyle(
               ControlStyles.AllPaintingInWmPaint |
               ControlStyles.OptimizedDoubleBuffer |
               ControlStyles.ResizeRedraw |
               ControlStyles.Selectable |
               ControlStyles.ContainerControl |
               ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.Opaque, false);
            this.UpdateStyles();
            this.BackColor = ColorTranslator.FromHtml("#FF87D68A");
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            this.lastWidth = this.Width;
            this.lastHeight = this.Height;
            this.lastLocation = this.Location;
            base.OnHandleCreated(e);
        }
        private bool isSizeChangeAble = true; //是否允许OnSizeChanged执行
        protected override void OnSizeChanged(EventArgs e)
        {
            if (isSizeChangeAble)
            {
                isSizeChangeAble = false;
                if (this.Width < this.lastWidth || this.Height < this.lastHeight)
                {
                    this.Width = Math.Min(this.Width, this.Height);
                    this.Height = Math.Min(this.Width, this.Height);
                    this.lastWidth = this.Width;
                    this.lastHeight = this.Height;
                    base.OnSizeChanged(e);
                    isSizeChangeAble = true;
                    return;
                }
                if (this.Width > this.lastWidth || this.Height > this.lastHeight)
                {
                    this.Width = Math.Max(this.Width, this.Height);
                    this.Height = Math.Max(this.Width, this.Height);
                    this.lastWidth = this.Width;
                    this.lastHeight = this.Height;
                    base.OnSizeChanged(e);
                    isSizeChangeAble = true;
                    return;
                }
                this.lastWidth = this.Width;
                this.lastHeight = this.Height;
                base.OnSizeChanged(e);
                isSizeChangeAble = true;
                return;
            }
        }
        private int circularWidth = 25;
        protected override void OnPaint(PaintEventArgs e)
        {
            if(Num == 35)
            {
                try
                {
                    e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
                    using (Pen p = new Pen(Brushes.LightGray, circularWidth))
                    {
                        p.LineJoin = LineJoin.Round;
                        e.Graphics.DrawEllipse(p, new Rectangle(new Point(e.ClipRectangle.X + circularWidth / 2, e.ClipRectangle.Y + circularWidth / 2), new Size(e.ClipRectangle.Width - 1 - circularWidth, e.ClipRectangle.Height - 1 - circularWidth)));
                    }
                    if (2 <= this.value && this.value <= 5)
                    {
                        this.mainColor = Color.LimeGreen;
                        using (Pen p = new Pen(new SolidBrush(this.mainColor), circularWidth))
                        {
                            p.LineJoin = LineJoin.Round;
                            e.Graphics.DrawArc(p, new Rectangle(new Point(e.ClipRectangle.X + circularWidth / 2, e.ClipRectangle.Y + circularWidth / 2), new Size(e.ClipRectangle.Width - 1 - circularWidth, e.ClipRectangle.Height - 1 - circularWidth)), -93, (float)((float)this.value * Num));
                        }
                    }
                    else if (5 < this.value && this.value < 7)
                    {
                        this.mainColor = Color.Orange;
                        using (Pen p = new Pen(new SolidBrush(this.mainColor), circularWidth))
                        {
                            p.LineJoin = LineJoin.Round;
                            e.Graphics.DrawArc(p, new Rectangle(new Point(e.ClipRectangle.X + circularWidth / 2, e.ClipRectangle.Y + circularWidth / 2), new Size(e.ClipRectangle.Width - 1 - circularWidth, e.ClipRectangle.Height - 1 - circularWidth)), -93, (float)((float)this.value * Num));
                        }
                    }
                    else if (this.value >= 7)
                    {
                        this.mainColor = Color.Red;
                        using (Pen p = new Pen(new SolidBrush(this.mainColor), circularWidth))
                        {
                            p.LineJoin = LineJoin.Round;
                            e.Graphics.DrawArc(p, new Rectangle(new Point(e.ClipRectangle.X + circularWidth / 2, e.ClipRectangle.Y + circularWidth / 2), new Size(e.ClipRectangle.Width - 1 - circularWidth, e.ClipRectangle.Height - 1 - circularWidth)), -93, (float)((float)this.value * Num));
                        }
                    }
                    else if (this.value < 2)
                    {
                        this.mainColor = Color.Blue;
                        using (Pen p = new Pen(new SolidBrush(this.mainColor), circularWidth))
                        {
                            p.LineJoin = LineJoin.Round;
                            e.Graphics.DrawArc(p, new Rectangle(new Point(e.ClipRectangle.X + circularWidth / 2, e.ClipRectangle.Y + circularWidth / 2), new Size(e.ClipRectangle.Width - 1 - circularWidth, e.ClipRectangle.Height - 1 - circularWidth)), -93, (float)((float)this.value * Num));
                        }
                    }
                    SizeF size = e.Graphics.MeasureString(this.value.ToString(), new Font("黑体", 15F, System.Drawing.FontStyle.Bold));
                    e.Graphics.DrawString(this.value.ToString(), new Font("黑体", 15F, System.Drawing.FontStyle.Bold), new SolidBrush(Color.DimGray), new Point(this.Width / 2 - (int)size.Width / 2 - 1, this.Height / 2 - (int)size.Height / 2 + 2));
                    e.Graphics.DrawString(Strtxt, new Font("仿宋", 9F, System.Drawing.FontStyle.Bold), new SolidBrush(Color.DimGray), new Point(this.Width / 2 + (int)size.Width / 2 - 8, this.Height / 2 - (int)size.Height + 8));
                }
                catch { }
            }else
            {
                try
                {
                    e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
                    using (Pen p = new Pen(Brushes.LightGray, circularWidth))
                    {
                        p.LineJoin = LineJoin.Round;
                        e.Graphics.DrawEllipse(p, new Rectangle(new Point(e.ClipRectangle.X + circularWidth / 2, e.ClipRectangle.Y + circularWidth / 2), new Size(e.ClipRectangle.Width - 1 - circularWidth, e.ClipRectangle.Height - 1 - circularWidth)));
                    }

                    this.mainColor = Color.Blue;
                    using (Pen p = new Pen(new SolidBrush(this.mainColor), circularWidth))
                    {
                        p.LineJoin = LineJoin.Round;
                        e.Graphics.DrawArc(p, new Rectangle(new Point(e.ClipRectangle.X + circularWidth / 2, e.ClipRectangle.Y + circularWidth / 2), new Size(e.ClipRectangle.Width - 1 - circularWidth, e.ClipRectangle.Height - 1 - circularWidth)), -93, (float)((float)this.value * Num));
                    }

                    SizeF size = e.Graphics.MeasureString(this.value.ToString(), new Font("黑体", 15F, System.Drawing.FontStyle.Bold));
                    e.Graphics.DrawString(this.value.ToString(), new Font("黑体", 15F, System.Drawing.FontStyle.Bold), new SolidBrush(Color.DimGray), new Point(this.Width / 2 - (int)size.Width / 2 - 1, this.Height / 2 - (int)size.Height / 2 + 2));
                    e.Graphics.DrawString(Strtxt, new Font("仿宋", 9F, System.Drawing.FontStyle.Bold), new SolidBrush(Color.DimGray), new Point(this.Width / 2 + (int)size.Width / 2 - 8, this.Height / 2 - (int)size.Height + 8));
                }catch
                {

                }

            }

        }
    }
}
//CircularProgressBarEx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace Insulin.Client.Circle
{
    public class CircularProgressBarEx : Control
    {
        public CircularProgressBar circularProgressBar = null;
        private string completeText = "";
        private string doingText = "";

        public double Value
        {
            get { return this.circularProgressBar.Value; }
            set
            {
                this.circularProgressBar.Value = value;
                this.Invalidate();
            }
        }

        public Color Color
        {
            get { return this.circularProgressBar.MainColor; }
            set
            {
                this.circularProgressBar.MainColor = value;
                this.Invalidate();
            }
        }

        [LocalizableAttribute(true)]
        public string CompleteText
        {
            get { return completeText; }
            set
            {
                completeText = value;
                this.Invalidate();
            }
        }

        [LocalizableAttribute(true)]
        public string DoingText
        {
            get { return doingText; }
            set
            {
                doingText = value;
                this.Invalidate();
            }
        }

        public CircularProgressBarEx(string Strtxt,int num)
        {
            circularProgressBar= new CircularProgressBar(Strtxt,num);
            //指定控件的样式和行为
            this.SetStyle(
               ControlStyles.AllPaintingInWmPaint |
               ControlStyles.OptimizedDoubleBuffer |
               ControlStyles.ResizeRedraw |
               ControlStyles.Selectable |
               ControlStyles.ContainerControl |
               ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.Opaque, false);
            this.UpdateStyles();
            this.BackColor = ColorTranslator.FromHtml("#FF87D68A");
            circularProgressBar.Dock = DockStyle.Top;
            this.Controls.Add(circularProgressBar);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
                SizeF size = e.Graphics.MeasureString(this.doingText, new Font("微软雅黑", 12F));
                e.Graphics.DrawString(this.doingText, new Font("微软雅黑", 12F), new SolidBrush(Color.Gray), new Point(this.Width / 2 - (int)size.Width / 2 - 1, this.Height - (int)size.Height - 5));
        }
    }
}

两个类可直接使用。
在WPF中使用时,在XMAL文件中需要添加DockPanel。

            <DockPanel HorizontalAlignment="Left" Height="130" LastChildFill="False" Margin="180,140,0,0" VerticalAlignment="Top" Width="130">
                <WindowsFormsHost Background="#0F59E860" x:Name="Temhost" Margin="0,0,0,0.2" Width="130"/>
            </DockPanel>

具体实现:

  CircularProgressBarEx circularProgressBarExTem = new CircularProgressBarEx("℃", 35);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洋洋脚踝的金铃响了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值