• 百分比进度控件----------WinForm控件开发系列


    该控件是继承于 Control 基类开发的。这个控件主要就是GDI绘图方面。

    百分比显示类型

      1         protected override void OnPaint(PaintEventArgs e)
      2         {
      3             base.OnPaint(e);
      4 
      5             Graphics g = e.Graphics;
      6             Rectangle rect = new Rectangle((int)this.ClientRectangle.X + ((int)this.ClientRectangle.Width / 2 - this.ArcRadius) + 1, (int)this.ClientRectangle.Y + ((int)this.ClientRectangle.Height / 2 - this.ArcRadius) + 1, this.ArcRadius * 2 - 2, this.ArcRadius * 2 - 2);
      7 
      8             #region 环形
      9             if (this.Type == PercentageType.Annulus)
     10             {
     11                 g.SmoothingMode = SmoothingMode.AntiAlias;
     12                 #region 背景
     13                 Pen arcback_pen = new Pen(this.ArcBackColor, this.ArcThickness);
     14                 g.DrawArc(arcback_pen, ControlCommom.TransformRectangleByPen(rect, this.ArcThickness), 270, 360);
     15                 arcback_pen.Dispose();
     16                 #endregion
     17 
     18                 if (this.ArcAnnulusBackColor != Color.Empty)
     19                 {
     20                     SolidBrush arcannulusback_sb = new SolidBrush(this.ArcAnnulusBackColor);
     21                     g.FillPie(arcannulusback_sb, rect.X + this.ArcThickness / 2, rect.Y + this.ArcThickness / 2, rect.Width - this.ArcThickness, rect.Height - this.ArcThickness, 270, 360);
     22                     arcannulusback_sb.Dispose();
     23                 }
     24 
     25                 #region 百分比值
     26                 Pen arc_pen = new Pen(this.ArcColor, this.ArcThickness);
     27                 if (this.ArcRound)
     28                 {
     29                     arc_pen.StartCap = LineCap.Round;
     30                     arc_pen.EndCap = LineCap.Round;
     31                 }
     32                 g.DrawArc(arc_pen, ControlCommom.TransformRectangleByPen(rect, this.ArcThickness), 270, 360 * this.Value);
     33                 arc_pen.Dispose();
     34                 #endregion
     35 
     36                 #region 文本
     37                 this.DrawValueText(g, rect);
     38                 #endregion
     39 
     40             }
     41             #endregion
     42             #region 扇形
     43             else if (this.Type == PercentageType.Sector)
     44             {
     45                 g.SmoothingMode = SmoothingMode.AntiAlias;
     46                 #region 背景
     47                 SolidBrush arcback_sb = new SolidBrush(this.ArcBackColor);
     48                 g.FillPie(arcback_sb, rect.X, rect.Y, rect.Width, rect.Height, 270, 360);
     49                 arcback_sb.Dispose();
     50                 #endregion
     51 
     52                 if (this.ArcAnnulusBackColor != Color.Empty)
     53                 {
     54                     SolidBrush arcannulusback_sb = new SolidBrush(this.ArcAnnulusBackColor);
     55                     g.FillPie(arcannulusback_sb, rect.X + this.ArcThickness / 2, rect.Y + this.ArcThickness / 2, rect.Width - this.ArcThickness, rect.Height - this.ArcThickness, 270, 360);
     56                     arcannulusback_sb.Dispose();
     57                 }
     58 
     59                 #region 百分比值
     60                 SolidBrush arc_sb = new SolidBrush(this.ArcColor);
     61                 g.FillPie(arc_sb, rect.X, rect.Y, rect.Width, rect.Height, 270, 360 * this.Value);
     62                 arc_sb.Dispose();
     63                 #endregion
     64 
     65                 #region 文本
     66                 this.DrawValueText(g, rect);
     67                 #endregion
     68 
     69             }
     70             #endregion
     71             #region 圆弧
     72             else if (this.Type == PercentageType.Arc)
     73             {
     74                 g.SmoothingMode = SmoothingMode.AntiAlias;
     75                 int start = this.ArcAngle == 360 ? 270 : 180 + (180 - this.ArcAngle) / 2;
     76 
     77                 #region 背景
     78                 Pen arcback_pen = new Pen(this.ArcBackColor, this.ArcThickness);
     79                 g.DrawArc(arcback_pen, ControlCommom.TransformRectangleByPen(rect, this.ArcThickness), start, this.ArcAngle);
     80                 arcback_pen.Dispose();
     81                 #endregion
     82 
     83                 #region 百分比值
     84                 Pen arc_pen = new Pen(this.ArcColor, this.ArcThickness);
     85                 if (this.ArcRound)
     86                 {
     87                     arc_pen.EndCap = LineCap.Round;
     88                 }
     89                 g.DrawArc(arc_pen, ControlCommom.TransformRectangleByPen(rect, this.ArcThickness), start, this.ArcAngle * this.Value);
     90                 arc_pen.Dispose();
     91                 #endregion
     92 
     93                 #region 文本
     94                 this.DrawValueText(g, rect);
     95                 #endregion
     96 
     97             }
     98             #endregion
     99             #region 方形
    100             else if (this.Type == PercentageType.Quadrangle)
    101             {
    102                 RectangleF rectf = new RectangleF();
    103                 rectf.Width = 10 + this.SquareWidth * 10;
    104                 rectf.Height = 10 + this.SquareWidth * 10;
    105                 rectf.X = this.ClientRectangle.X + (this.ClientRectangle.Width - rectf.Width) / 2;
    106                 rectf.Y = this.ClientRectangle.Bottom - rectf.Height;
    107 
    108                 #region 背景
    109                 SolidBrush arcback_sb = new SolidBrush(this.ArcBackColor);
    110                 g.FillRectangle(arcback_sb, rectf);
    111                 arcback_sb.Dispose();
    112                 #endregion
    113 
    114                 #region 百分比值
    115                 SolidBrush arc_sb = new SolidBrush(this.ArcColor);
    116                 string str = this.Value.ToString("F4");
    117                 int index = str.IndexOf('.');
    118                 int row = int.Parse(str.Substring(index + 1, 1));
    119                 RectangleF row_rectf = new RectangleF();
    120                 row_rectf.Width = rectf.Width;
    121                 row_rectf.Height = row * 1 + row * this.SquareWidth;
    122                 row_rectf.X = rectf.X;
    123                 row_rectf.Y = rectf.Bottom - row_rectf.Height;
    124                 g.FillRectangle(arc_sb, row_rectf);
    125 
    126                 int col = int.Parse(str.Substring(index + 2, 1));
    127                 if (col > 0)
    128                 {
    129                     RectangleF col_rectf = new RectangleF();
    130                     col_rectf.Width = col * 1 + col * this.SquareWidth;
    131                     col_rectf.Height = this.SquareWidth + 1;
    132                     col_rectf.X = rectf.X;
    133                     col_rectf.Y = row_rectf.Y - col_rectf.Height;
    134                     g.FillRectangle(arc_sb, col_rectf);
    135                 }
    136 
    137                 arc_sb.Dispose();
    138                 #endregion
    139 
    140                 #region 边框
    141                 Pen arcbackborder_pen = new Pen(this.SquareBorderColor, 1);
    142                 for (int i = 0; i < 11; i++)//
    143                 {
    144                     g.DrawLine(arcbackborder_pen, rectf.X, rectf.Y + i * 1 + i * this.SquareWidth, rectf.Right, rectf.Y + i * 1 + i * this.SquareWidth);
    145                 }
    146                 for (int i = 0; i < 11; i++)//
    147                 {
    148                     g.DrawLine(arcbackborder_pen, rectf.X + i * 1 + i * this.SquareWidth, rectf.Y, rectf.X + i * 1 + i * this.SquareWidth, rectf.Bottom);
    149                 }
    150                 arcbackborder_pen.Dispose();
    151                 #endregion
    152 
    153                 #region 百分比值文本
    154                 string value_str = (this.Value * 100).ToString("F0") + "%";
    155                 StringFormat value_sf = new StringFormat();
    156                 value_sf.FormatFlags = StringFormatFlags.NoWrap;
    157                 value_sf.Alignment = StringAlignment.Center;
    158                 value_sf.Trimming = StringTrimming.None;
    159                 Size value_size = g.MeasureString(value_str, this.ValueFont, new Size(), value_sf).ToSize();
    160                 SolidBrush value_sb = new SolidBrush(this.ValueColor);
    161                 g.DrawString(value_str, this.ValueFont, value_sb, new RectangleF(rectf.X + (rectf.Width - value_size.Width) / 2, rectf.Y - value_size.Height, value_size.Width, value_size.Height), value_sf);
    162                 value_sb.Dispose();
    163                 value_sf.Dispose();
    164                 #endregion
    165 
    166             }
    167             #endregion
    168             #region 条形
    169             else if (this.Type == PercentageType.Bar)
    170             {
    171                 if (this.ArcRound)
    172                 {
    173                     g.SmoothingMode = SmoothingMode.AntiAlias;
    174                 }
    175                 RectangleF rectf = new RectangleF();
    176                 rectf.Width = this.ClientRectangle.Width - (this.ArcRound ? this.ArcThickness : 0);
    177                 rectf.Height = this.ArcThickness;
    178                 rectf.X = this.ClientRectangle.X + (this.ArcRound ? this.ArcThickness / 2 : 0);
    179                 rectf.Y = this.ClientRectangle.Bottom - rectf.Height;
    180 
    181                 #region 背景
    182                 Pen arcback_pen = new Pen(this.ArcBackColor, this.ArcThickness);
    183                 if (this.ArcRound)
    184                 {
    185                     arcback_pen.StartCap = LineCap.Round;
    186                     arcback_pen.EndCap = LineCap.Round;
    187                 }
    188                 g.DrawLine(arcback_pen, rectf.X, rectf.Bottom - rectf.Height / 2, rectf.Right, rectf.Bottom - rectf.Height / 2);
    189                 arcback_pen.Dispose();
    190                 #endregion
    191 
    192                 #region 百分比值
    193                 Pen arc_pen = new Pen(this.ArcColor, this.ArcThickness);
    194                 if (this.ArcRound)
    195                 {
    196                     arc_pen.StartCap = LineCap.Round;
    197                     arc_pen.EndCap = LineCap.Round;
    198                 }
    199                 g.DrawLine(arc_pen, rectf.X, rectf.Bottom - rectf.Height / 2, (int)(rectf.Right * this.Value), rectf.Bottom - rectf.Height / 2);
    200                 arc_pen.Dispose();
    201                 #endregion
    202 
    203                 #region 百分比值文本
    204                 string value_str = (this.Value * 100).ToString("F0") + "%";
    205                 StringFormat value_sf = new StringFormat();
    206                 value_sf.FormatFlags = StringFormatFlags.NoWrap;
    207                 value_sf.Alignment = StringAlignment.Center;
    208                 value_sf.Trimming = StringTrimming.None;
    209                 Size value_size = g.MeasureString(value_str, this.ValueFont, new Size(), value_sf).ToSize();
    210                 SolidBrush value_sb = new SolidBrush(this.ValueColor);
    211                 g.DrawString(value_str, this.ValueFont, value_sb, new RectangleF(rectf.X + (rectf.Width - value_size.Width) / 2, rectf.Y - value_size.Height, value_size.Width, value_size.Height), value_sf);
    212                 value_sb.Dispose();
    213                 value_sf.Dispose();
    214                 #endregion
    215 
    216             }
    217             #endregion
    218 
    219         }

    新增的类如下

    新增属性如下

    控件库的源码已整体发布到gitee,下载地址:(花木兰控件库)https://gitee.com/tlmbem/hml

  • 相关阅读:
    Vue目录查询
    Vue框架学习(五)
    Vue框架学习(四)
    Vue框架学习(三)
    Vue框架学习(二)
    Vue框架学习(一)
    python中定义函数和参数的传递问题
    数据处理
    关于在程序中 文件新生成 在用os.system()程序对新生成的文件处理 举个栗子 如下:
    c++2008 并行配置文件和获取字典的所有key的方法
  • 原文地址:https://www.cnblogs.com/tlmbem/p/11427476.html
Copyright © 2020-2023  润新知