前提 入行已經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+畫的,不瞭解自行百度學習下
開始
添加一個類UCTrackBar,繼承Control
添加屬性
1 [Description("值改變事件"), Category("自定義")] 2 public event EventHandler ValueChanged; 3 4 private int dcimalDigits = 0; 5 6 [Description("值小數精確位數"), Category("自定義")] 7 public int DcimalDigits 8 { 9 get { return dcimalDigits; } 10 set { dcimalDigits = value; } 11 } 12 13 private float lineWidth = 10; 14 15 [Description("線寬度"), Category("自定義")] 16 public float LineWidth 17 { 18 get { return lineWidth; } 19 set { lineWidth = value; } 20 } 21 22 private float minValue = 0; 23 24 [Description("最小值"), Category("自定義")] 25 public float MinValue 26 { 27 get { return minValue; } 28 set 29 { 30 if (minValue > m_value) 31 return; 32 minValue = value; 33 this.Refresh(); 34 } 35 } 36 37 private float maxValue = 100; 38 39 [Description("最大值"), Category("自定義")] 40 public float MaxValue 41 { 42 get { return maxValue; } 43 set 44 { 45 if (value < m_value) 46 return; 47 maxValue = value; 48 this.Refresh(); 49 } 50 } 51 52 private float m_value = 0; 53 54 [Description("值"), Category("自定義")] 55 public float Value 56 { 57 get { return this.m_value; } 58 set 59 { 60 if (value > maxValue || value < minValue) 61 return; 62 var v = (float)Math.Round((double)value, dcimalDigits); 63 if (value == v) 64 return; 65 this.m_value = v; 66 this.Refresh(); 67 if (ValueChanged != null) 68 { 69 ValueChanged(this, null); 70 } 71 } 72 } 73 74 private Color m_lineColor = Color.FromArgb(255, 77, 59); 75 76 [Description("線顏色"), Category("自定義")] 77 public Color LineColor 78 { 79 get { return m_lineColor; } 80 set 81 { 82 m_lineColor = value; 83 this.Refresh(); 84 } 85 } 86 RectangleF m_lineRectangle; 87 RectangleF m_trackRectangle;
重繪
1 protected override void OnPaint(PaintEventArgs e) 2 { 3 base.OnPaint(e); 4 Graphics g = e.Graphics; 5 g.SetGDIHigh(); 6 m_lineRectangle = new RectangleF(lineWidth, (this.Size.Height - lineWidth) / 2, this.Size.Width - lineWidth * 2, lineWidth); 7 GraphicsPath pathLine = ControlHelper.CreateRoundedRectanglePath(m_lineRectangle, 5); 8 g.FillPath(new SolidBrush(m_lineColor), pathLine); 9 m_trackRectangle = new RectangleF(m_lineRectangle.Left - lineWidth + (((float)m_value / (float)(maxValue - minValue)) * (this.Size.Width - lineWidth * 2)), (this.Size.Height - lineWidth * 2) / 2, lineWidth * 2, lineWidth * 2); 10 g.FillEllipse(new SolidBrush(m_lineColor), m_trackRectangle); 11 g.FillEllipse(Brushes.White, new RectangleF(m_trackRectangle.X + m_trackRectangle.Width / 4, m_trackRectangle.Y + m_trackRectangle.Height / 4, m_trackRectangle.Width / 2, m_trackRectangle.Height / 2)); 12 }
處理拖動
1 public UCTrackBar() 2 { 3 this.Size = new Size(250, 30); 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 this.MouseDown += UCTrackBar_MouseDown; 11 this.MouseMove += UCTrackBar_MouseMove; 12 this.MouseUp += UCTrackBar_MouseUp; 13 } 14 15 bool blnDown = false; 16 void UCTrackBar_MouseDown(object sender, MouseEventArgs e) 17 { 18 if (m_lineRectangle.Contains(e.Location) || m_trackRectangle.Contains(e.Location)) 19 { 20 blnDown = true; 21 Value = ((float)e.Location.X / (float)this.Width) * (maxValue - minValue); 22 } 23 } 24 void UCTrackBar_MouseMove(object sender, MouseEventArgs e) 25 { 26 if (blnDown) 27 { 28 Value = ((float)e.Location.X / (float)this.Width) * (maxValue - minValue); 29 } 30 } 31 void UCTrackBar_MouseUp(object sender, MouseEventArgs e) 32 { 33 blnDown = false; 34 }
完整代碼
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows.Forms; 6 using System.Drawing; 7 using System.Drawing.Drawing2D; 8 using System.ComponentModel; 9 10 namespace HZH_Controls.Controls 11 { 12 public class UCTrackBar : Control 13 { 14 [Description("值改變事件"), Category("自定義")] 15 public event EventHandler ValueChanged; 16 17 private int dcimalDigits = 0; 18 19 [Description("值小數精確位數"), Category("自定義")] 20 public int DcimalDigits 21 { 22 get { return dcimalDigits; } 23 set { dcimalDigits = value; } 24 } 25 26 private float lineWidth = 10; 27 28 [Description("線寬度"), Category("自定義")] 29 public float LineWidth 30 { 31 get { return lineWidth; } 32 set { lineWidth = value; } 33 } 34 35 private float minValue = 0; 36 37 [Description("最小值"), Category("自定義")] 38 public float MinValue 39 { 40 get { return minValue; } 41 set 42 { 43 if (minValue > m_value) 44 return; 45 minValue = value; 46 this.Refresh(); 47 } 48 } 49 50 private float maxValue = 100; 51 52 [Description("最大值"), Category("自定義")] 53 public float MaxValue 54 { 55 get { return maxValue; } 56 set 57 { 58 if (value < m_value) 59 return; 60 maxValue = value; 61 this.Refresh(); 62 } 63 } 64 65 private float m_value = 0; 66 67 [Description("值"), Category("自定義")] 68 public float Value 69 { 70 get { return this.m_value; } 71 set 72 { 73 if (value > maxValue || value < minValue) 74 return; 75 var v = (float)Math.Round((double)value, dcimalDigits); 76 if (value == v) 77 return; 78 this.m_value = v; 79 this.Refresh(); 80 if (ValueChanged != null) 81 { 82 ValueChanged(this, null); 83 } 84 } 85 } 86 87 private Color m_lineColor = Color.FromArgb(255, 77, 59); 88 89 [Description("線顏色"), Category("自定義")] 90 public Color LineColor 91 { 92 get { return m_lineColor; } 93 set 94 { 95 m_lineColor = value; 96 this.Refresh(); 97 } 98 } 99 RectangleF m_lineRectangle; 100 RectangleF m_trackRectangle; 101 102 public UCTrackBar() 103 { 104 this.Size = new Size(250, 30); 105 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 106 this.SetStyle(ControlStyles.DoubleBuffer, true); 107 this.SetStyle(ControlStyles.ResizeRedraw, true); 108 this.SetStyle(ControlStyles.Selectable, true); 109 this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); 110 this.SetStyle(ControlStyles.UserPaint, true); 111 this.MouseDown += UCTrackBar_MouseDown; 112 this.MouseMove += UCTrackBar_MouseMove; 113 this.MouseUp += UCTrackBar_MouseUp; 114 } 115 116 bool blnDown = false; 117 void UCTrackBar_MouseDown(object sender, MouseEventArgs e) 118 { 119 if (m_lineRectangle.Contains(e.Location) || m_trackRectangle.Contains(e.Location)) 120 { 121 blnDown = true; 122 Value = ((float)e.Location.X / (float)this.Width) * (maxValue - minValue); 123 } 124 } 125 void UCTrackBar_MouseMove(object sender, MouseEventArgs e) 126 { 127 if (blnDown) 128 { 129 Value = ((float)e.Location.X / (float)this.Width) * (maxValue - minValue); 130 } 131 } 132 void UCTrackBar_MouseUp(object sender, MouseEventArgs e) 133 { 134 blnDown = false; 135 } 136 137 138 protected override void OnPaint(PaintEventArgs e) 139 { 140 base.OnPaint(e); 141 Graphics g = e.Graphics; 142 g.SetGDIHigh(); 143 m_lineRectangle = new RectangleF(lineWidth, (this.Size.Height - lineWidth) / 2, this.Size.Width - lineWidth * 2, lineWidth); 144 GraphicsPath pathLine = ControlHelper.CreateRoundedRectanglePath(m_lineRectangle, 5); 145 g.FillPath(new SolidBrush(m_lineColor), pathLine); 146 m_trackRectangle = new RectangleF(m_lineRectangle.Left - lineWidth + (((float)m_value / (float)(maxValue - minValue)) * (this.Size.Width - lineWidth * 2)), (this.Size.Height - lineWidth * 2) / 2, lineWidth * 2, lineWidth * 2); 147 g.FillEllipse(new SolidBrush(m_lineColor), m_trackRectangle); 148 g.FillEllipse(Brushes.White, new RectangleF(m_trackRectangle.X + m_trackRectangle.Width / 4, m_trackRectangle.Y + m_trackRectangle.Height / 4, m_trackRectangle.Width / 2, m_trackRectangle.Height / 2)); 149 } 150 } 151 }View Code
最後的話
如果你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星星吧