• (四十二)c#Winform自定义控件-进度条扩展-HZHControls


    官网

    http://www.hzhcontrols.com

    前提

    入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

    GitHub:https://github.com/kwwwvagaa/NetWinformControl

    码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

    如果觉得写的还行,请点个 star 支持一下吧

    欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

    NuGet

    Install-Package HZH_Controls

    目录

    https://www.cnblogs.com/bfyx/p/11364884.html

    用处及效果

    准备工作

    这个是基于(四十一)c#Winform自定义控件-进度条 扩展的,如果你还没有了解,请先移步了解一下

    开始

    添加一个用户控件,命名UCProcessLineExt

    属性

     1   [Description("值变更事件"), Category("自定义")]
     2         public event EventHandler ValueChanged;
     3 
     4         [Description("当前属性"), Category("自定义")]
     5         public int Value
     6         {
     7             set
     8             {
     9                 ucProcessLine1.Value = value;
    10                 Refresh();
    11             }
    12             get
    13             {
    14                 return ucProcessLine1.Value;
    15             }
    16         }
    17 
    18 
    19 
    20         [Description("最大值"), Category("自定义")]
    21         public int MaxValue
    22         {
    23             get { return ucProcessLine1.MaxValue; }
    24             set
    25             {
    26                 ucProcessLine1.MaxValue = value;
    27                 Refresh();
    28             }
    29         }
    30 
    31 
    32         [Description("值进度条颜色"), Category("自定义")]
    33         public Color ValueColor
    34         {
    35             get { return ucProcessLine1.ValueColor; }
    36             set
    37             {
    38                 ucProcessLine1.ValueColor = value;
    39                 Refresh();
    40             }
    41         }
    42 
    43 
    44         [Description("值背景色"), Category("自定义")]
    45         public Color ValueBGColor
    46         {
    47             get { return ucProcessLine1.ValueBGColor; }
    48             set
    49             {
    50                 ucProcessLine1.ValueBGColor = value;
    51                 Refresh();
    52             }
    53         }
    54 
    55 
    56         [Description("边框颜色"), Category("自定义")]
    57         public Color BorderColor
    58         {
    59             get { return ucProcessLine1.BorderColor; }
    60             set
    61             {
    62                 ucProcessLine1.BorderColor = value;
    63                 Refresh();
    64             }
    65         }
    66 
    67         [Description("值字体"), Category("自定义")]
    68         public override Font Font
    69         {
    70             get
    71             {
    72                 return ucProcessLine1.Font;
    73             }
    74             set
    75             {
    76                 ucProcessLine1.Font = value;
    77                 Refresh();
    78             }
    79         }
    80 
    81         [Description("值块颜色"), Category("自定义")]
    82         public override System.Drawing.Color ForeColor
    83         {
    84             get
    85             {
    86                 return base.ForeColor;
    87             }
    88             set
    89             {
    90                 base.ForeColor = value;
    91                 Refresh();
    92             }
    93         }

    重绘

     1  protected override void OnPaint(PaintEventArgs e)
     2         {
     3             base.OnPaint(e);
     4             e.Graphics.SetGDIHigh();
     5             float fltIndex = (float)this.ucProcessLine1.Value / (float)this.ucProcessLine1.MaxValue;
     6 
     7             int x = (int)(fltIndex * this.ucProcessLine1.Width + this.ucProcessLine1.Location.X - 15) - 2;
     8             GraphicsPath path = new GraphicsPath();
     9             Rectangle rect = new Rectangle(x, 1, 30, 20);
    10             int cornerRadius = 2;
    11             path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
    12             path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
    13             path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
    14             path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
    15             path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
    16             path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.Right - cornerRadius * 2 - 5, rect.Bottom);//
    17             path.AddLine(rect.Right - cornerRadius * 2 - 5, 21, x + 15, ucProcessLine1.Location.Y);
    18             path.AddLine(x + 15, ucProcessLine1.Location.Y, rect.X + cornerRadius * 2 + 5, 21);
    19             path.AddLine(rect.X + cornerRadius * 2 + 5, 20, rect.X + cornerRadius * 2, rect.Bottom);//
    20             path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
    21             path.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);//
    22             path.CloseFigure();
    23 
    24             e.Graphics.FillPath(new SolidBrush(ForeColor), path);
    25 
    26             string strValue = ((float)Value / (float)MaxValue).ToString("0%");
    27             System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
    28             e.Graphics.DrawString(strValue, Font, new SolidBrush(Color.White), new PointF(x + (30 - sizeF.Width) / 2+1, (20 - sizeF.Height) / 2 + 1));
    29         }

    全部代码

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Drawing;
      5 using System.Data;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 using System.Drawing.Drawing2D;
     10 
     11 namespace HZH_Controls.Controls
     12 {
     13     public partial class UCProcessLineExt : UserControl
     14     {
     15         [Description("值变更事件"), Category("自定义")]
     16         public event EventHandler ValueChanged;
     17 
     18         [Description("当前属性"), Category("自定义")]
     19         public int Value
     20         {
     21             set
     22             {
     23                 ucProcessLine1.Value = value;
     24                 Refresh();
     25             }
     26             get
     27             {
     28                 return ucProcessLine1.Value;
     29             }
     30         }
     31 
     32 
     33 
     34         [Description("最大值"), Category("自定义")]
     35         public int MaxValue
     36         {
     37             get { return ucProcessLine1.MaxValue; }
     38             set
     39             {
     40                 ucProcessLine1.MaxValue = value;
     41                 Refresh();
     42             }
     43         }
     44 
     45 
     46         [Description("值进度条颜色"), Category("自定义")]
     47         public Color ValueColor
     48         {
     49             get { return ucProcessLine1.ValueColor; }
     50             set
     51             {
     52                 ucProcessLine1.ValueColor = value;
     53                 Refresh();
     54             }
     55         }
     56 
     57 
     58         [Description("值背景色"), Category("自定义")]
     59         public Color ValueBGColor
     60         {
     61             get { return ucProcessLine1.ValueBGColor; }
     62             set
     63             {
     64                 ucProcessLine1.ValueBGColor = value;
     65                 Refresh();
     66             }
     67         }
     68 
     69 
     70         [Description("边框颜色"), Category("自定义")]
     71         public Color BorderColor
     72         {
     73             get { return ucProcessLine1.BorderColor; }
     74             set
     75             {
     76                 ucProcessLine1.BorderColor = value;
     77                 Refresh();
     78             }
     79         }
     80 
     81         [Description("值字体"), Category("自定义")]
     82         public override Font Font
     83         {
     84             get
     85             {
     86                 return ucProcessLine1.Font;
     87             }
     88             set
     89             {
     90                 ucProcessLine1.Font = value;
     91                 Refresh();
     92             }
     93         }
     94 
     95         [Description("值块颜色"), Category("自定义")]
     96         public override System.Drawing.Color ForeColor
     97         {
     98             get
     99             {
    100                 return base.ForeColor;
    101             }
    102             set
    103             {
    104                 base.ForeColor = value;
    105                 Refresh();
    106             }
    107         }
    108 
    109         public UCProcessLineExt()
    110         {
    111             InitializeComponent();
    112             this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    113             this.SetStyle(ControlStyles.DoubleBuffer, true);
    114             this.SetStyle(ControlStyles.ResizeRedraw, true);
    115             this.SetStyle(ControlStyles.Selectable, true);
    116             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
    117             this.SetStyle(ControlStyles.UserPaint, true);
    118             ucProcessLine1.ValueChanged += ucProcessLine1_ValueChanged;
    119         }
    120 
    121         void ucProcessLine1_ValueChanged(object sender, EventArgs e)
    122         {
    123             if (ValueChanged != null)
    124             {
    125                 ValueChanged(this, e);
    126             }
    127         }
    128 
    129         protected override void OnPaint(PaintEventArgs e)
    130         {
    131             base.OnPaint(e);
    132             e.Graphics.SetGDIHigh();
    133             float fltIndex = (float)this.ucProcessLine1.Value / (float)this.ucProcessLine1.MaxValue;
    134 
    135             int x = (int)(fltIndex * this.ucProcessLine1.Width + this.ucProcessLine1.Location.X - 15) - 2;
    136             GraphicsPath path = new GraphicsPath();
    137             Rectangle rect = new Rectangle(x, 1, 30, 20);
    138             int cornerRadius = 2;
    139             path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
    140             path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
    141             path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
    142             path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
    143             path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
    144             path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.Right - cornerRadius * 2 - 5, rect.Bottom);//
    145             path.AddLine(rect.Right - cornerRadius * 2 - 5, 21, x + 15, ucProcessLine1.Location.Y);
    146             path.AddLine(x + 15, ucProcessLine1.Location.Y, rect.X + cornerRadius * 2 + 5, 21);
    147             path.AddLine(rect.X + cornerRadius * 2 + 5, 20, rect.X + cornerRadius * 2, rect.Bottom);//
    148             path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
    149             path.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);//
    150             path.CloseFigure();
    151 
    152             e.Graphics.FillPath(new SolidBrush(ForeColor), path);
    153 
    154             string strValue = ((float)Value / (float)MaxValue).ToString("0%");
    155             System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
    156             e.Graphics.DrawString(strValue, Font, new SolidBrush(Color.White), new PointF(x + (30 - sizeF.Width) / 2+1, (20 - sizeF.Height) / 2 + 1));
    157         }
    158     }
    159 }
    View Code
     1 namespace HZH_Controls.Controls
     2 {
     3     partial class UCProcessLineExt
     4     {
     5         /// <summary> 
     6         /// 必需的设计器变量。
     7         /// </summary>
     8         private System.ComponentModel.IContainer components = null;
     9 
    10         /// <summary> 
    11         /// 清理所有正在使用的资源。
    12         /// </summary>
    13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    14         protected override void Dispose(bool disposing)
    15         {
    16             if (disposing && (components != null))
    17             {
    18                 components.Dispose();
    19             }
    20             base.Dispose(disposing);
    21         }
    22 
    23         #region 组件设计器生成的代码
    24 
    25         /// <summary> 
    26         /// 设计器支持所需的方法 - 不要
    27         /// 使用代码编辑器修改此方法的内容。
    28         /// </summary>
    29         private void InitializeComponent()
    30         {
    31             this.ucProcessLine1 = new HZH_Controls.Controls.UCProcessLine();
    32             this.SuspendLayout();
    33             // 
    34             // ucProcessLine1
    35             // 
    36             this.ucProcessLine1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    37             | System.Windows.Forms.AnchorStyles.Left) 
    38             | System.Windows.Forms.AnchorStyles.Right)));
    39             this.ucProcessLine1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
    40             this.ucProcessLine1.Font = new System.Drawing.Font("Arial Unicode MS", 10F);
    41             this.ucProcessLine1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
    42             this.ucProcessLine1.Location = new System.Drawing.Point(18, 33);
    43             this.ucProcessLine1.MaxValue = 100;
    44             this.ucProcessLine1.Name = "ucProcessLine1";
    45             this.ucProcessLine1.Size = new System.Drawing.Size(399, 16);
    46             this.ucProcessLine1.TabIndex = 0;
    47             this.ucProcessLine1.Text = "ucProcessLine1";
    48             this.ucProcessLine1.Value = 0;
    49             this.ucProcessLine1.ValueBGColor = System.Drawing.Color.White;
    50             this.ucProcessLine1.ValueColor = System.Drawing.Color.FromArgb(((int)(((byte)(73)))), ((int)(((byte)(119)))), ((int)(((byte)(232)))));
    51             this.ucProcessLine1.ValueTextType = HZH_Controls.Controls.ValueTextType.None;
    52             // 
    53             // UCProcessLineExt
    54             // 
    55             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
    56             this.BackColor = System.Drawing.Color.Transparent;
    57             this.Controls.Add(this.ucProcessLine1);
    58             this.Name = "UCProcessLineExt";
    59             this.Size = new System.Drawing.Size(434, 50);
    60             this.ResumeLayout(false);
    61 
    62         }
    63 
    64         #endregion
    65 
    66         private UCProcessLine ucProcessLine1;
    67     }
    68 }
    View Code

    最后的话

    如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

  • 相关阅读:
    js的event对象 详解
    RestSharp使用详解(1)调用阿里巴巴开放存储服务
    RestSharp使用详解(2)RestSharp的BUG和不足
    WF实例学习笔记:(2)通过Workflow 调用 WCF Data Services 获取数据
    译文:SQL Azure客户端瞬态错误处理最佳实践
    Windbg 基本命令
    RestSharp使用详解(3)OSS文件上传的问题
    Transient Fault Handling and Retry Logic: 瞬间错误处理——重试
    推荐一本免费的Node.js电子书(台湾)
    CSS导航菜单应用滑动门技术的玻璃效果菜单
  • 原文地址:https://www.cnblogs.com/bfyx/p/11392351.html
Copyright © 2020-2023  润新知