前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_contr ...
前提
入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果覺得寫的還行,請點個 star 支持一下吧
麻煩博客下方點個【推薦】,謝謝
NuGet
Install-Package HZH_Controls
目錄
https://www.cnblogs.com/bfyx/p/11364884.html
用處及效果
準備工作
這個用到GDI+畫的,請先瞭解一下GDI+
還有用到了基類控制項UCControlBase來控制圓角和背景色,如果還不瞭解請移步查看
另外用到了水波控制項UCWave,如果不瞭解請移步查看
開始
添加一個用戶控制項UCProcessWave,繼承UCControlBase
一些屬性
1 private bool m_isRectangle = false; 2 [Description("是否矩形"), Category("自定義")] 3 public bool IsRectangle 4 { 5 get { return m_isRectangle; } 6 set 7 { 8 m_isRectangle = value; 9 if (value) 10 { 11 base.ConerRadius = 10; 12 } 13 else 14 { 15 base.ConerRadius = Math.Min(this.Width, this.Height); 16 } 17 } 18 } 19 #region 不再使用的父類屬性 English:Parent class attributes that are no longer used 20 [Browsable(false)] 21 public new int ConerRadius 22 { 23 get; 24 set; 25 } 26 [Browsable(false)] 27 public new bool IsRadius 28 { 29 get; 30 set; 31 } 32 33 [Browsable(false)] 34 public new Color FillColor 35 { 36 get; 37 set; 38 } 39 #endregion 40 41 42 [Description("值變更事件"), Category("自定義")] 43 public event EventHandler ValueChanged; 44 int m_value = 0; 45 [Description("當前屬性"), Category("自定義")] 46 public int Value 47 { 48 set 49 { 50 if (value > m_maxValue) 51 m_value = m_maxValue; 52 else if (value < 0) 53 m_value = 0; 54 else 55 m_value = value; 56 if (ValueChanged != null) 57 ValueChanged(this, null); 58 ucWave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + ucWave1.WaveHeight; 59 Refresh(); 60 } 61 get 62 { 63 return m_value; 64 } 65 } 66 67 private int m_maxValue = 100; 68 69 [Description("最大值"), Category("自定義")] 70 public int MaxValue 71 { 72 get { return m_maxValue; } 73 set 74 { 75 if (value < m_value) 76 m_maxValue = m_value; 77 else 78 m_maxValue = value; 79 Refresh(); 80 } 81 } 82 83 public override Font Font 84 { 85 get 86 { 87 return base.Font; 88 } 89 set 90 { 91 base.Font = value; 92 } 93 } 94 95 public override Color ForeColor 96 { 97 get 98 { 99 return base.ForeColor; 100 } 101 set 102 { 103 base.ForeColor = value; 104 } 105 } 106 107 [Description("值顏色"), Category("自定義")] 108 public Color ValueColor 109 { 110 get { return this.ucWave1.WaveColor; } 111 set 112 { 113 this.ucWave1.WaveColor = value; 114 } 115 } 116 117 [Description("邊框寬度"), Category("自定義")] 118 public override int RectWidth 119 { 120 get 121 { 122 return base.RectWidth; 123 } 124 set 125 { 126 if (value < 4) 127 base.RectWidth = 4; 128 else 129 base.RectWidth = value; 130 } 131 }
構造函數一些設置
1 public UCProcessWave() 2 { 3 InitializeComponent(); 4 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 5 this.SetStyle(ControlStyles.DoubleBuffer, true); 6 this.SetStyle(ControlStyles.ResizeRedraw, true); 7 this.SetStyle(ControlStyles.Selectable, true); 8 this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); 9 this.SetStyle(ControlStyles.UserPaint, true); 10 base.IsRadius = true; 11 base.IsShowRect = false; 12 RectWidth = 4; 13 RectColor = Color.White; 14 ucWave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + ucWave1.WaveHeight; 15 this.SizeChanged += UCProcessWave_SizeChanged; 16 this.ucWave1.OnPainted += ucWave1_Painted; 17 base.ConerRadius = Math.Min(this.Width, this.Height); 18 }
重繪
1 protected override void OnPaint(PaintEventArgs e) 2 { 3 base.OnPaint(e); 4 e.Graphics.SetGDIHigh(); 5 if (!m_isRectangle) 6 { 7 //這裡曲線救國,因為設置了控制項區域導致的毛邊,通過畫一個沒有毛邊的圓遮擋 8 SolidBrush solidBrush = new SolidBrush(Color.White); 9 e.Graphics.DrawEllipse(new Pen(solidBrush, 2), new Rectangle(-1, -1, this.Width + 2, this.Height + 2)); 10 } 11 string strValue = ((double)m_value / (double)m_maxValue).ToString("0.%"); 12 System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font); 13 e.Graphics.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / 2, (this.Height - sizeF.Height) / 2 + 1)); 14 }
波形控制項重繪時處理
1 void ucWave1_Painted(object sender, PaintEventArgs e) 2 { 3 e.Graphics.SetGDIHigh(); 4 if (IsShowRect) 5 { 6 if (m_isRectangle) 7 { 8 Color rectColor = RectColor; 9 Pen pen = new Pen(rectColor, (float)RectWidth); 10 Rectangle clientRectangle = new Rectangle(0, this.ucWave1.Height - this.Height, this.Width, this.Height); 11 GraphicsPath graphicsPath = new GraphicsPath(); 12 graphicsPath.AddArc(clientRectangle.X, clientRectangle.Y, 10, 10, 180f, 90f); 13 graphicsPath.AddArc(clientRectangle.Width - 10 - 1, clientRectangle.Y, 10, 10, 270f, 90f); 14 graphicsPath.AddArc(clientRectangle.Width - 10 - 1, clientRectangle.Bottom - 10 - 1, 10, 10, 0f, 90f); 15 graphicsPath.AddArc(clientRectangle.X, clientRectangle.Bottom - 10 - 1, 10, 10, 90f, 90f); 16 graphicsPath.CloseFigure(); 17 e.Graphics.DrawPath(pen, graphicsPath); 18 } 19 else 20 { 21 SolidBrush solidBrush = new SolidBrush(RectColor); 22 e.Graphics.DrawEllipse(new Pen(solidBrush, RectWidth), new Rectangle(0, this.ucWave1.Height - this.Height, this.Width, this.Height)); 23 } 24 } 25 26 if (!m_isRectangle) 27 { 28 //這裡曲線救國,因為設置了控制項區域導致的毛邊,通過畫一個沒有毛邊的圓遮擋 29 SolidBrush solidBrush1 = new SolidBrush(Color.White); 30 e.Graphics.DrawEllipse(new Pen(solidBrush1, 2), new Rectangle(-1, this.ucWave1.Height - this.Height - 1, this.Width + 2, this.Height + 2)); 31 } 32 string strValue = ((double)m_value / (double)m_maxValue).ToString("0.%"); 33 System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font); 34 e.Graphics.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / 2, (this.ucWave1.Height - this.Height) + (this.Height - sizeF.Height) / 2)); 35 }
不知道你們有沒有註意這句話
//這裡曲線救國,因為設置了控制項區域導致的毛邊,通過畫一個沒有毛邊的圓遮擋
因為設置原價導致了區域毛邊,所有畫個沒有毛邊的邊框覆蓋之
完整代碼
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 UCProcessWave : UCControlBase 14 { 15 private bool m_isRectangle = false; 16 [Description("是否矩形"), Category("自定義")] 17 public bool IsRectangle 18 { 19 get { return m_isRectangle; } 20 set 21 { 22 m_isRectangle = value; 23 if (value) 24 { 25 base.ConerRadius = 10; 26 } 27 else 28 { 29 base.ConerRadius = Math.Min(this.Width, this.Height); 30 } 31 } 32 } 33 #region 不再使用的父類屬性 English:Parent class attributes that are no longer used 34 [Browsable(false)] 35 public new int ConerRadius 36 { 37 get; 38 set; 39 } 40 [Browsable(false)] 41 public new bool IsRadius 42 { 43 get; 44 set; 45 } 46 47 [Browsable(false)] 48 public new Color FillColor 49 { 50 get; 51 set; 52 } 53 #endregion 54 55 56 [Description("值變更事件"), Category("自定義")] 57 public event EventHandler ValueChanged; 58 int m_value = 0; 59 [Description("當前屬性"), Category("自定義")] 60 public int Value 61 { 62 set 63 { 64 if (value > m_maxValue) 65 m_value = m_maxValue; 66 else if (value < 0) 67 m_value = 0; 68 else 69 m_value = value; 70 if (ValueChanged != null) 71 ValueChanged(this, null); 72 ucWave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + ucWave1.WaveHeight; 73 Refresh(); 74 } 75 get 76 { 77 return m_value; 78 } 79 } 80 81 private int m_maxValue = 100; 82 83 [Description("最大值"), Category("自定義")] 84 public int MaxValue 85 { 86 get { return m_maxValue; } 87 set 88 { 89 if (value < m_value) 90 m_maxValue = m_value; 91 else 92 m_maxValue = value; 93 Refresh(); 94 } 95 } 96 97 public override Font Font 98 { 99 get 100 { 101 return base.Font; 102 } 103 set 104 { 105 base.Font = value; 106 } 107 } 108 109 public override Color ForeColor 110 { 111 get 112 { 113 return base.ForeColor; 114 } 115 set 116 { 117 base.ForeColor = value; 118 } 119 } 120 121 [Description("值顏色"), Category("自定義")] 122 public Color ValueColor 123 { 124 get { return this.ucWave1.WaveColor; } 125 set 126 { 127 this.ucWave1.WaveColor = value; 128 } 129 } 130 131 [Description("邊框寬度"), Category("自定義")] 132 public override int RectWidth 133 { 134 get 135 { 136 return base.RectWidth; 137 } 138 set 139 { 140 if (value < 4) 141 base.RectWidth = 4; 142 else 143 base.RectWidth = value; 144 } 145 } 146 147 public UCProcessWave() 148 { 149 InitializeComponent(); 150 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 151 this.SetStyle(ControlStyles.DoubleBuffer, true); 152 this.SetStyle(ControlStyles.ResizeRedraw, true); 153 this.SetStyle(ControlStyles.Selectable, true); 154 this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); 155 this.SetStyle(ControlStyles.UserPaint, true); 156 base.IsRadius = true; 157 base.IsShowRect = false; 158 RectWidth = 4; 159 RectColor = Color.White; 160 ucWave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + ucWave1.WaveHeight; 161 this.SizeChanged += UCProcessWave_SizeChanged; 162 this.ucWave1.OnPainted += ucWave1_Painted; 163 base.ConerRadius = Math.Min(this.Width, this.Height); 164 } 165 166 void ucWave1_Painted(object sender, PaintEventArgs e) 167 { 168 e.Graphics.SetGDIHigh(); 169 if (IsShowRect) 170 { 171 if (m_isRectangle) 172 { 173 Color rectColor = RectColor; 174 Pen pen = new Pen(rectColor, (float)RectWidth); 175 Rectangle clientRectangle = new Rectangle(0, this.ucWave1.Height - this.Height, this.Width, this.Height); 176 GraphicsPath graphicsPath = new GraphicsPath(); 177 graphicsPath.AddArc(clientRectangle.X, clientRectangle.Y, 10, 10, 180f, 90f); 178 graphicsPath.AddArc(clientRectangle.Width - 10 - 1, clientRectangle.Y, 10, 10, 270f, 90f); 179 graphicsPath.AddArc(clientRectangle.Width - 10 - 1, clientRectangle.Bottom - 10 - 1, 10, 10, 0f, 90f); 180 graphicsPath.AddArc(clientRectangle.X, clientRectangle.Bottom - 10 - 1, 10, 10, 90f, 90f); 181 graphicsPath.CloseFigure(); 182 e.Graphics.DrawPath(pen, graphicsPath); 183 } 184 else 185 { 186 SolidBrush solidBrush = new SolidBrush(RectColor); 187 e.Graphics.DrawEllipse(new Pen(solidBrush, RectWidth), new Rectangle(0, this.ucWave1.Height - this.Height, this.Width, this.Height)); 188 } 189 } 190 191 if (!m_isRectangle) 192 { 193 //這裡曲線救國,因為設置了控制項區域導致的毛邊,通過畫一個沒有毛邊的圓遮擋 194 SolidBrush solidBrush1 = new SolidBrush(Color.White); 195 e.Graphics.DrawEllipse(new Pen(solidBrush1, 2), new Rectangle(-1, this.ucWave1.Height - this.Height - 1, this.Width + 2, this.Height + 2)); 196 } 197 string strValue = ((double)m_value / (double)m_maxValue).ToString("0.%"); 198 System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font); 199 e.Graphics.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / 2, (this.ucWave1.Height - this.Height) + (this.Height - sizeF.Height) / 2)); 200 } 201 202 void UCProcessWave_SizeChanged(object sender, EventArgs e) 203 { 204 if (!m_isRectangle) 205 { 206 base.ConerRadius = Math.Min(this.Width, this.Height); 207 if (