premise
It's been 7 or 8 years, and I've always wanted to make a beautiful set of custom controls, so I've got this series of articles.
GitHub: https://github.com/kwwwvagaa/NetWinformControl
Code cloud: https://gitee.com/kwwwvagaa/net_winform_custom_control.git
If you think it's OK, please click star to support it.
Welcome to exchange and discuss: Penguins 568015492
Thank you for your recommendation.
NuGet
Install-Package HZH_Controls
Catalog
https://blog.csdn.net/kwwwvagaa/article/details/100586547
Utility and effect
Preparation
This is also drawn with GDI + and should be the simplest control. I didn't intend to write a single article, but it's also a control, so I wrote it here.
start
Add a class UCPond to inherit UserControl
attribute
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("Maximum value"), Category("custom")] 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("value"), Category("custom")] 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("Pool wall color"), Category("custom")] 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("Width of tank wall"), Category("custom")] 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("Liquid color"), Category("custom")] 96 public Color LiquidColor 97 { 98 get { return liquidColor; } 99 set { liquidColor = value; } 100 }
Repaint
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)); } //Edge drawing 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); }
Complete code
1 // *********************************************************************** 2 // Assembly : HZH_Controls 3 // Created : 2019-09-06 4 // 5 // *********************************************************************** 6 // <copyright file="UCPond.cs"> 7 // Copyright by Huang Zhenghui (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("Maximum value"), Category("custom")] 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("value"), Category("custom")] 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("Pool wall color"), Category("custom")] 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("Width of tank wall"), Category("custom")] 111 public int WallWidth 112 { 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("Liquid color"), Category("custom")] 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 /// Raise < see cref= "E: System. Windows. Forms. Control. Paint" /> event. 150 /// </summary> 151 /// <param name="e">Seecref="T:System.Windows.Forms.PaintEventArgs"/> containing event data. </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 //Edge drawing 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 }
Last words
If you like, please go to https://gitee.com/kwwwvagaa/net_winform_custom_control Spot a star.