码迷,mamicode.com
首页 > 其他好文 > 详细

进度条控件

时间:2019-07-25 14:41:30      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:system   背景   sum   pad   sig   height   cte   grid   paint   

技术图片

  [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
  }

 

进度条控件

标签:system   背景   sum   pad   sig   height   cte   grid   paint   

原文地址:https://www.cnblogs.com/tlmbem/p/11211639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!