前提

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

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

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

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

欢迎前来交流探讨: 企鹅群568015492 
<https://shang.qq.com/wpa/qunwpa?idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d>

麻烦博客下方点个【推荐】,谢谢

NuGet
Install-Package HZH_Controls
目录

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

用处及效果



 

 

准备工作

这个也是用GDI+画的,应该算是最简单的控件了,本来不打算单独写一篇文章的,但是好歹也是个控件吧,于是就写这里了

开始

添加一个类UCPond ,继承UserControl

属性
1 /// <summary> 2 /// The maximum value 3 /// </summary> 4 private decimal
maxValue =100; 5 6 /// <summary> 7 /// Gets or sets the maximum value. 8 ///
</summary> 9 /// <value>The maximum value.</value> 10 [Description("最大值"),
Category("自定义")] 11 public decimal MaxValue 12 { 13 get { return maxValue; }
14 set 15 { 16 if (value < m_value) 17 return; 18 maxValue = value; 19
Refresh(); 20 } 21 } 22 23 /// <summary> 24 /// The m value 25 ///
</summary> 26 private decimal m_value = 0; 27 28 /// <summary> 29 /// Gets
or sets the value. 30 /// </summary> 31 /// <value>The value.</value> 32
[Description("值"), Category("自定义")] 33 public decimal Value 34 { 35 get {
return m_value; } 36 set 37 { 38 if (value < 0) 39 return; 40 if (value >
maxValue) 41 m_value = maxValue; 42 else 43 m_value = value; 44 Refresh();
45 } 46 } 47 48 /// <summary> 49 /// The wall color 50 /// </summary> 51
private Color wallColor = Color.FromArgb(255, 77, 59); 52 53 /// <summary> 54
/// Gets or sets the color of the wall. 55 /// </summary> 56 /// <value>The
color of the wall.</value> 57 [Description("池壁颜色"), Category("自定义")] 58 public
Color WallColor 59 { 60 get { return wallColor; } 61 set 62 { 63
wallColor = value; 64 Refresh(); 65 } 66 } 67 68 /// <summary> 69 ///
The wall width 70 /// </summary> 71 private int wallWidth = 2; 72 73 ///
<summary> 74 /// Gets or sets the width of the wall. 75 /// </summary> 76 ///
<value>The width of the wall.</value> 77 [Description("池壁宽度"), Category("自定义")]
78 public int WallWidth 79 { 80 get { return wallWidth; } 81 set 82 { 83
if (value <= 0) 84 return; 85 wallWidth = value; 86 Refresh(); 87 } 88 }
89 90 /// <summary> 91 /// The liquid color 92 /// </summary> 93 private
Color liquidColor = Color.FromArgb(3, 169, 243); 94 95 [Description("液体颜色"),
Category("自定义")] 96 public Color LiquidColor 97 { 98 get { return
liquidColor; } 99 set { liquidColor = value; } 100 }
重绘
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if
(Height <=0) return; var g = e.Graphics; g.SetGDIHigh(); int intHeight = (int
)(m_value / maxValue *this.Height); if (intHeight != 0) { g.FillRectangle(new
SolidBrush(liquidColor),new Rectangle(0, this.Height - intHeight, this.Width,
intHeight)); }//划边 g.FillRectangle(new SolidBrush(wallColor), 0, 0, wallWidth,
this.Height); g.FillRectangle(new SolidBrush(wallColor), 0, this.Height -
wallWidth,this.Width, wallWidth); g.FillRectangle(new SolidBrush(wallColor),
this.Width - wallWidth-1, 0, wallWidth, this.Height); }
完整代码
1 // ***********************************************************************
2 // Assembly : HZH_Controls 3 // Created : 2019-09-06 4 // 5 //
*********************************************************************** 6 //
<copyright file="UCPond.cs"> 7 // Copyright by Huang Zhenghui(黄正辉) All, QQ
group:568015492 QQ:623128629 Email:623128629@qq.com 8 // </copyright> 9 // 10
// Blog: https://www.cnblogs.com/bfyx 11 // GitHub:
https://github.com/kwwwvagaa/NetWinformControl 12 // gitee:
https://gitee.com/kwwwvagaa/net_winform_custom_control.git 13 // 14 // If you
use this code, please keep this note. 15 //
*********************************************************************** 16
using System; 17 using System.Collections.Generic; 18 using System.Linq; 19
using System.Text; 20 using System.Windows.Forms; 21 using System.Drawing; 22
using System.Drawing.Drawing2D; 23 using System.ComponentModel; 24 25
namespace HZH_Controls.Controls 26 { 27 /// <summary> 28 /// Class UCPond.
29 /// Implements the <see cref="System.Windows.Forms.UserControl" /> 30 ///
</summary> 31 /// <seealso cref="System.Windows.Forms.UserControl" /> 32
public class UCPond : UserControl 33 { 34 /// <summary> 35 /// The maximum
value 36 /// </summary> 37 private decimal maxValue = 100; 38 39 ///
<summary> 40 /// Gets or sets the maximum value. 41 /// </summary> 42 ///
<value>The maximum value.</value> 43 [Description("最大值"), Category("自定义")] 44
public decimal MaxValue 45 { 46 get { return maxValue; } 47 set 48 { 49
if (value < m_value) 50 return; 51 maxValue = value; 52 Refresh(); 53 }
54 } 55 56 /// <summary> 57 /// The m value 58 /// </summary> 59 private
decimal m_value = 0; 60 61 /// <summary> 62 /// Gets or sets the value. 63
/// </summary> 64 /// <value>The value.</value> 65 [Description("值"),
Category("自定义")] 66 public decimal Value 67 { 68 get { return m_value; } 69
set 70 { 71 if (value < 0) 72 return; 73 if (value > maxValue) 74 m_value
= maxValue; 75 else 76 m_value = value; 77 Refresh(); 78 } 79 } 80 81
/// <summary> 82 /// The wall color 83 /// </summary> 84 private Color
wallColor = Color.FromArgb(255, 77, 59); 85 86 /// <summary> 87 /// Gets or
sets the color of the wall. 88 /// </summary> 89 /// <value>The color of the
wall.</value> 90 [Description("池壁颜色"), Category("自定义")] 91 public Color
WallColor 92 { 93 get { return wallColor; } 94 set 95 { 96 wallColor =
value; 97 Refresh(); 98 } 99 } 100 101 /// <summary> 102 /// The wall width
103 /// </summary> 104 private int wallWidth = 2; 105 106 /// <summary> 107 ///
Gets or sets the width of the wall.108 /// </summary> 109 /// <value>The width
of the wall.</value> 110 [Description("池壁宽度"), Category("自定义")] 111 public int
WallWidth112 { 113 get { return wallWidth; } 114 set 115 { 116 if (value <= 0)
117 return; 118 wallWidth = value; 119 Refresh(); 120 } 121 } 122 123 ///
<summary> 124 /// The liquid color 125 /// </summary> 126 private Color
liquidColor = Color.FromArgb(3, 169, 243); 127 128 [Description("液体颜色"),
Category("自定义")] 129 public Color LiquidColor 130 { 131 get { return
liquidColor; }132 set { liquidColor = value; } 133 } 134 /// <summary> 135 ///
Initializes a new instance of the<see cref="UCPond"/> class. 136 /// </summary>
137 public UCPond() 138 { 139 this.SetStyle(ControlStyles.AllPaintingInWmPaint,
true); 140 this.SetStyle(ControlStyles.DoubleBuffer, true); 141 this
.SetStyle(ControlStyles.ResizeRedraw,true); 142 this
.SetStyle(ControlStyles.Selectable,true); 143 this
.SetStyle(ControlStyles.SupportsTransparentBackColor,true); 144 this
.SetStyle(ControlStyles.UserPaint,true); 145 this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.None;146 this.Size = new Size(150, 50); 147
}148 /// <summary> 149 /// 引发 <see cref="E:System.Windows.Forms.Control.Paint"
/> 事件。 150 /// </summary> 151 /// <param name="e">包含事件数据的 <see
cref="T:System.Windows.Forms.PaintEventArgs" />。</param> 152 protected override
void OnPaint(PaintEventArgs e) 153 { 154 base.OnPaint(e); 155 if (Height <= 0)
156 return; 157 var g = e.Graphics; 158 g.SetGDIHigh(); 159 int intHeight = (
int)(m_value / maxValue * this.Height); 160 if (intHeight != 0) 161 { 162
g.FillRectangle(new SolidBrush(liquidColor), new Rectangle(0, this.Height -
intHeight,this.Width, intHeight)); 163 } 164 //划边 165 g.FillRectangle(new
SolidBrush(wallColor),0, 0, wallWidth, this.Height); 166 g.FillRectangle(new
SolidBrush(wallColor),0, this.Height - wallWidth, this.Width, wallWidth); 167
g.FillRectangle(new SolidBrush(wallColor), this.Width - wallWidth-1, 0,
wallWidth,this.Height); 168 } 169 } 170 }
 

最后的话

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