进度条控件

  [ToolboxItem(true)]
  public partial class ProgressExt : Control
  {

    private Orientation orientation = Orientation.Horizontal;
    /// <summary>
    /// 控件方向
    /// </summary>
    [DefaultValue(Orientation.Horizontal)]
    [Description("控件方向")]
    public Orientation Orientation
    {
      get { return this.orientation; }
      set
      {
        this.orientation = value;
        this.Invalidate();
      }

    }

    private List<GroupParam> groupParamList;
    /// <summary>
    /// 颜色尺度设置
    /// </summary>
    [Description("颜色尺度设置")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public List<GroupParam> GroupParamList
    {
      get
      {
        if (this.groupParamList == null)
          this.groupParamList = new List<GroupParam>();
        return this.groupParamList;
      }
      set
      {
        this.groupParamList = value;
        this.Invalidate();
      }
    }

    private Color gridsColor = Color.YellowGreen;
    /// <summary>
    /// 网格默认颜色
    /// </summary>
    [DefaultValue(typeof(Color), "YellowGreen")]
    [Description("网格默认颜色")]
    public Color GridsColor
    {
      get { return this.gridsColor; }
      set
      {
        this.gridsColor = value;
        this.Invalidate();
      }
    }

    private int gridsWidth = 6;
    /// <summary>
    /// 网格默认宽度
    /// </summary>
    [DefaultValue(6)]
    [Description("网格默认宽度")]
    public int GridsWidth
    {
      get { return this.gridsWidth; }
      set
      {
        this.gridsWidth = value;
        this.Invalidate();
      }
    }

    private float progressValue = 0.0f;
    /// <summary>
    /// 进度值(0-1)
    /// </summary>
    [DefaultValue(0.0f)]
    [Description("进度值")]
    public float ProgressValue
    {
      get { return this.progressValue; }
      set
      {
        if (this.progressValue == value)
          return;
        if (value > 1)
          value = 1;
        if (value < 0)
          value = 0;
        this.progressValue = value;
        this.Invalidate();
      }
    }

    private bool showProgressValue = false;
    /// <summary>
    /// 是否显示进度值
    /// </summary>
    [DefaultValue(false)]
    [Description("是否显示进度值")]
    public bool ShowProgressValue
    {
      get { return this.showProgressValue; }
      set
      {
        if (this.showProgressValue == value)
          return;
        this.showProgressValue = value;
        this.Invalidate();
      }
    }

    private ColorType progressColorType = ColorType.Level;
    /// <summary>
    /// 进度颜色类型
    /// </summary>
    [DefaultValue(ColorType.Level)]
    [Description("进度颜色类型")]
    public ColorType ProgressColorType
    {
      get { return this.progressColorType; }
      set
      {
        this.progressColorType = value;
        this.Invalidate();
      }
    }

    private Color gradientStartColor = Color.Yellow;
    /// <summary>
    /// 渐变开始颜色
    /// </summary>
    [DefaultValue(typeof(Color), "Yellow")]
    [Description("渐变开始颜色")]
    public Color GradientStartColor
    {
      get { return this.gradientStartColor; }
      set
      {
        this.gradientStartColor = value;
        this.Invalidate();
      }
    }

    private Color gradientEndColor = Color.YellowGreen;
    /// <summary>
    /// 渐变结束颜色
    /// </summary>
    [DefaultValue(typeof(Color), "YellowGreen")]
    [Description("渐变结束颜色")]
    public Color GradientEndColor
    {
      get { return this.gradientEndColor; }
      set
      {
        this.gradientEndColor = value;
        this.Invalidate();
      }
    }

    protected override Size DefaultSize
    {
      get
      {
        return new Size(150, 35); ;
      }
    }

    public ProgressExt()
    {
      SetStyle(ControlStyles.UserPaint, true);
      SetStyle(ControlStyles.AllPaintingInWmPaint, true);
      SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
      SetStyle(ControlStyles.ResizeRedraw, true);
      SetStyle(ControlStyles.SupportsTransparentBackColor, true);
      InitializeComponent();
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
      base.OnPaint(e);

      Graphics g = e.Graphics;
      g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
      SizeF s = g.MeasureString("100%", this.Font);

      RectangleF rectf = new RectangleF(0, 0, this.Width - 1, this.Height - 1);
      PointF fontp = new PointF(0, 0);
      if (this.showProgressValue)
      {
        if (this.orientation == System.Windows.Forms.Orientation.Horizontal)
        {
          rectf = new RectangleF(0, 0, this.Width - s.Width - 1, this.Height - 1);
          fontp = new PointF(rectf.Width, (rectf.Height - s.Height) / 2);
        }
        else
        {
          rectf = new RectangleF(0, s.Height, this.Width - 1, this.Height - s.Height - 1);
          fontp = new PointF((rectf.Width - s.Width) / 2, 0);
        }
      }

      Pen pen = new Pen(this.gridsColor);
      #region 生成画笔
      Brush sb = null;
      if (this.ProgressColorType == ColorType.Level)
      {
        Color color = Color.FromArgb(255 - this.BackColor.R, 255 - this.BackColor.G, 255 - this.BackColor.B);
        for (int i = this.GroupParamList.Count - 1; i > -1; i--)
        {
          if (this.progressValue >= this.GroupParamList[i].interval)
          {
            color = this.GroupParamList[i].color;
            break;
          }
        }
        sb = new SolidBrush(color);
      }
      else
      {
        sb = new LinearGradientBrush(rectf, this.gradientStartColor, this.gradientEndColor, this.orientation == System.Windows.Forms.Orientation.Horizontal ? 0.0f : -90.0f, true);
      }
      #endregion

      if (this.orientation == System.Windows.Forms.Orientation.Horizontal)
      {
        float wa = rectf.Width * this.progressValue;
        g.FillRectangle(sb, 0.0f, 0.0f, wa, rectf.Height);
        g.DrawLine(pen, 0, rectf.Height / 2, rectf.Width, rectf.Height / 2);
        int count = (int)Math.Ceiling(rectf.Width / (float)this.gridsWidth);
        for (int i = 0; i < count; i++)
        {
          float x = this.gridsWidth * i;
          g.DrawLine(pen, x, 0, x, rectf.Height);
        }
      }
      else
      {
        float hv = rectf.Height * this.progressValue;
        g.FillRectangle(sb, 0.0f, rectf.Bottom - hv, rectf.Width, hv);//进度值背景
        g.DrawLine(pen, rectf.Width / 2, rectf.Y, rectf.Width / 2, rectf.Bottom);//中线
        int count = (int)Math.Ceiling(rectf.Height / (float)this.gridsWidth);
        for (int i = count - 1; i > -1; i--)
        {
          int y = (int)(rectf.Bottom - this.gridsWidth * i);
          g.DrawLine(pen, 0, y, rectf.Width, y);
        }
      }
      g.DrawRectangle(pen, rectf.X, rectf.Y, rectf.Width, rectf.Height);//外边框

      #region 进度值
      if (this.showProgressValue)
      {
        string str = ((int)(this.progressValue * 100)).ToString().PadLeft(3, ' ') + "%";
        g.DrawString(str, this.Font, new SolidBrush(ForeColor), fontp.X, fontp.Y);
      }
      #endregion

      sb.Dispose();
      pen.Dispose();
    }


  }

  /// <summary>
  /// 颜色尺度设置
  /// </summary>
  public class GroupParam
  {
    /// <summary>
    /// 从该值开始的背景颜色(0-1)
    /// </summary>
    public float interval { get; set; }
    /// <summary>
    /// 背景颜色
    /// </summary>
    public Color color { get; set; }
  }

  /// <summary>
  /// 进度颜色类型
  /// </summary>
  public enum ColorType
  {
    /// <summary>
    /// 渐变
    /// </summary>
    Gradient,
    /// <summary>
    /// 级别
    /// </summary>
    Level
  }

 

转载于:https://www.cnblogs.com/tlmbem/p/11211639.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值