前提 入行已經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
用處及效果
準備工作
使用GID+畫的,不瞭解的話請自行百度
開始
添加一個類UCLEDNum ,繼承UserControl
將數字拆分為單獨的字,然後根據顯示位置進行畫出來,將顯示位置定義為如下所示
/* 顯示位置序號 * ****1*** * * * * 6 2 * * * * ****7*** * * * * 5 3 * * * * ****4*** */
從上面可以看出,定義了1-7的位置,然後定義一個數字對應的顯示列表
1 private static Dictionary<char, int[]> m_nums = new Dictionary<char, int[]>(); 2 static UCLEDNum() 3 { 4 m_nums['0'] = new int[] { 1, 2, 3, 4, 5, 6 }; 5 m_nums['1'] = new int[] { 2, 3 }; 6 m_nums['2'] = new int[] { 1, 2, 5, 4, 7 }; 7 m_nums['3'] = new int[] { 1, 2, 7, 3, 4 }; 8 m_nums['4'] = new int[] { 2, 3, 6, 7 }; 9 m_nums['5'] = new int[] { 1, 6, 7, 3, 4 }; 10 m_nums['6'] = new int[] { 1, 6, 5, 4, 3, 7 }; 11 m_nums['7'] = new int[] { 1, 2, 3 }; 12 m_nums['8'] = new int[] { 1, 2, 3, 4, 5, 6, 7 }; 13 m_nums['9'] = new int[] { 1, 2, 3, 4, 7, 6 }; 14 m_nums['-'] = new int[] { 7 }; 15 m_nums[':'] = new int[0]; 16 m_nums['.'] = new int[0]; 17 }
你看到了還有“-”,“:”,“.”這3個符號,是為了時間和數字時候使用
然後定義一個矩形區域來用作繪畫區域,並且在SizeChanged事件中賦值
Rectangle m_drawRect = Rectangle.Empty; void LEDNum_SizeChanged(object sender, EventArgs e) { m_drawRect = new Rectangle(1, 1, this.Width - 2, this.Height - 2); }
然後就是幾個屬性
1 private char m_value = '0'; 2 3 [Description("值"), Category("自定義")] 4 public char Value 5 { 6 get { return m_value; } 7 set 8 { 9 if (!m_nums.ContainsKey(value)) 10 { 11 return; 12 } 13 if (m_value != value) 14 { 15 m_value = value; 16 Refresh(); 17 } 18 } 19 } 20 21 private int m_lineWidth = 8; 22 23 [Description("線寬度,為了更好的顯示效果,請使用偶數"), Category("自定義")] 24 public int LineWidth 25 { 26 get { return m_lineWidth; } 27 set 28 { 29 m_lineWidth = value; 30 Refresh(); 31 } 32 } 33 34 [Description("顏色"), Category("自定義")] 35 public override System.Drawing.Color ForeColor 36 { 37 get 38 { 39 return base.ForeColor; 40 } 41 set 42 { 43 base.ForeColor = value; 44 } 45 }
最重要的重繪
1 protected override void OnPaint(PaintEventArgs e) 2 { 3 base.OnPaint(e); 4 e.Graphics.SetGDIHigh(); 5 if (m_value == '.') 6 { 7 Rectangle r2 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Bottom - m_lineWidth * 2, m_lineWidth, m_lineWidth); 8 e.Graphics.FillRectangle(new SolidBrush(ForeColor), r2); 9 } 10 else if (m_value == ':') 11 { 12 Rectangle r1 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Top + (m_drawRect.Height / 2 - m_lineWidth) / 2, m_lineWidth, m_lineWidth); 13 e.Graphics.FillRectangle(new SolidBrush(ForeColor), r1); 14 Rectangle r2 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Top + (m_drawRect.Height / 2 - m_lineWidth) / 2 + m_drawRect.Height / 2, m_lineWidth, m_lineWidth); 15 e.Graphics.FillRectangle(new SolidBrush(ForeColor), r2); 16 } 17 else 18 { 19 int[] vs = m_nums[m_value]; 20 if (vs.Contains(1)) 21 { 22 GraphicsPath path = new GraphicsPath(); 23 path.AddLines(new Point[] 24 { 25 new Point(m_drawRect.Left + 2, m_drawRect.Top), 26 new Point(m_drawRect.Right - 2, m_drawRect.Top), 27 new Point(m_drawRect.Right - m_lineWidth-2, m_drawRect.Top+m_lineWidth), 28 new Point(m_drawRect.Left + m_lineWidth+2, m_drawRect.Top+m_lineWidth), 29 new Point(m_drawRect.Left + 2, m_drawRect.Top) 30 }); 31 path.CloseAllFigures(); 32 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 33 } 34 35 if (vs.Contains(2)) 36 { 37 GraphicsPath path = new GraphicsPath(); 38 path.AddLines(new Point[] 39 { 40 new Point(m_drawRect.Right, m_drawRect.Top), 41 new Point(m_drawRect.Right, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2), 42 new Point(m_drawRect.Right-m_lineWidth/2, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2+m_lineWidth/2), 43 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2), 44 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Top+m_lineWidth), 45 new Point(m_drawRect.Right, m_drawRect.Top) 46 }); 47 path.CloseAllFigures(); 48 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 49 } 50 51 if (vs.Contains(3)) 52 { 53 GraphicsPath path = new GraphicsPath(); 54 path.AddLines(new Point[] 55 { 56 new Point(m_drawRect.Right, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 57 new Point(m_drawRect.Right, m_drawRect.Bottom), 58 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Bottom-m_lineWidth), 59 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 60 new Point(m_drawRect.Right-m_lineWidth/2, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2-m_lineWidth/2), 61 new Point(m_drawRect.Right, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 62 }); 63 path.CloseAllFigures(); 64 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 65 } 66 67 if (vs.Contains(4)) 68 { 69 GraphicsPath path = new GraphicsPath(); 70 path.AddLines(new Point[] 71 { 72 new Point(m_drawRect.Left + 2, m_drawRect.Bottom), 73 new Point(m_drawRect.Right - 2, m_drawRect.Bottom), 74 new Point(m_drawRect.Right - m_lineWidth-2, m_drawRect.Bottom-m_lineWidth), 75 new Point(m_drawRect.Left + m_lineWidth+2, m_drawRect.Bottom-m_lineWidth), 76 new Point(m_drawRect.Left + 2, m_drawRect.Bottom) 77 }); 78 path.CloseAllFigures(); 79 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 80 } 81 82 if (vs.Contains(5)) 83 { 84 GraphicsPath path = new GraphicsPath(); 85 path.AddLines(new Point[] 86 { 87 new Point(m_drawRect.Left, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 88 new Point(m_drawRect.Left, m_drawRect.Bottom), 89 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Bottom-m_lineWidth), 90 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 91 new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2-m_lineWidth/2), 92 new Point(m_drawRect.Left, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2), 93 }); 94 path.CloseAllFigures(); 95 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 96 } 97 98 99 if (vs.Contains(6)) 100 { 101 GraphicsPath path = new GraphicsPath(); 102 path.AddLines(new Point[] 103 { 104 new Point(m_drawRect.Left, m_drawRect.Top), 105 new Point(m_drawRect.Left, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2), 106 new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2+m_lineWidth/2), 107 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2), 108 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Top+m_lineWidth), 109 new Point(m_drawRect.Left, m_drawRect.Top) 110 }); 111 path.CloseAllFigures(); 112 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 113 } 114 115 if (vs.Contains(7)) 116 { 117 GraphicsPath path = new GraphicsPath(); 118 path.AddLines(new Point[] 119 { 120 new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Height/2+1), 121 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Height/2-m_lineWidth/2+1), 122 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Height/2-m_lineWidth/2+1), 123 new Point(m_drawRect.Right-m_lineWidth/2, m_drawRect.Height/2+1), 124 new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Height/2+m_lineWidth/2+1), 125 new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Height/2+m_lineWidth/2+1), 126 new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Height/2+1) 127 }); 128 path.CloseAllFigures(); 129 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 130 } 131 } 132 }
完工,看下完整代碼和效果
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 /* 顯示位置序號 13 * ****1*** 14 * * * 15 * 6 2 16 * * * 17 * ****7*** 18 * * * 19 * 5 3 20 * * * 21 * ****4*** 22 */ 23 public class UCLEDNum : UserControl 24 { 25 Rectangle m_drawRect = Rectangle.Empty; 26 27 private static Dictionary<char, int[]> m_nums = new Dictionary<char, int[]>(); 28 static UCLEDNum() 29 { 30 m_nums['0'] = new int[] { 1, 2, 3, 4, 5, 6 }; 31 m_nums['1'] = new int[] { 2, 3 }; 32 m_nums['2'] = new int[] { 1, 2, 5, 4, 7 }; 33 m_nums['3'] = new int[] { 1, 2, 7, 3, 4 }; 34 m_nums['4'] = new int[] { 2, 3, 6, 7 }; 35 m_nums['5'] = new int[] { 1, 6, 7, 3, 4 }; 36 m_nums['6'] = new int[] { 1, 6, 5, 4, 3, 7 }; 37 m_nums['7'] = new int[] { 1, 2, 3 }; 38 m_nums['8'] = new int[] { 1, 2, 3, 4, 5, 6, 7 }; 39 m_nums['9'] = new int[] { 1, 2, 3, 4, 7, 6 }; 40 m_nums['-'] = new int[] { 7 }; 41 m_nums[':'] = new int[0]; 42 m_nums['.'] = new int[0]; 43 } 44 45 46 public UCLEDNum() 47 { 48 SizeChanged += LEDNum_SizeChanged; 49 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; 50 Size = new System.Drawing.Size(40, 70); 51 if (m_drawRect == Rectangle.Empty) 52 m_drawRect = new Rectangle(1, 1, this.Width - 2, this.Height - 2); 53 } 54 55 void LEDNum_SizeChanged(object sender, EventArgs e) 56 { 57 m_drawRect = new Rectangle(1, 1, this.Width - 2, this.Height - 2); 58 } 59 60 private char m_value = '0'; 61 62 [Description("值"), Category("自定義")] 63 public char Value 64 { 65 get { return m_value; } 66 set 67 { 68 if (!m_nums.ContainsKey(value)) 69 { 70 return; 71 } 72 if (m_value != value) 73 { 74 m_value = value; 75 Refresh(); 76 } 77 } 78 } 79 80 private int m_lineWidth = 8; 81 82 [Description("線寬度,為了更好的顯示效果,請使用偶數"), Category("自定義")] 83 public int LineWidth 84 { 85 get { return m_lineWidth; } 86 set 87 { 88 m_lineWidth = value; 89 Refresh(); 90 } 91 } 92 93 [Description("顏色"), Category("自定義")] 94 public override System.Drawing.Color ForeColor 95 { 96 get 97 { 98 return base.ForeColor; 99 } 100 set 101 { 102 base.ForeColor = value; 103 } 104 } 105 106 protected override void OnPaint(PaintEventArgs e) 107 { 108 base.OnPaint(e); 109 e.Graphics.SetGDIHigh(); 110 if (m_value == '.') 111 { 112 Rectangle r2 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Bottom - m_lineWidth * 2, m_lineWidth, m_lineWidth); 113 e.Graphics.FillRectangle(new SolidBrush(ForeColor), r2); 114 } 115 else if (m_value == ':') 116 { 117 Rectangle r1 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Top + (m_drawRect.Height / 2 - m_lineWidth) / 2, m_lineWidth, m_lineWidth); 118 e.Graphics.FillRectangle(new SolidBrush(ForeColor), r1); 119 Rectangle r2 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Top + (m_drawRect.Height / 2 - m_lineWidth) / 2 + m_drawRect.Height / 2, m_lineWidth, m_lineWidth); 120 e.Graphics.FillRectangle(new SolidBrush(ForeColor), r2); 121 } 122 else 123 { 124 int[] vs = m_nums[m_value]; 125 if (vs.Contains(1)) 126 { 127 GraphicsPath path = new GraphicsPath(); 128 path.AddLines(new Point[] 129 { 130 new Point(m_drawRect.Left + 2, m_drawRect.Top), 131 new Point(m_drawRect.Right - 2, m_drawRect.Top), 132 new Point(m_drawRect.Right - m_lineWidth-2, m_drawRect.Top+m_lineWidth), 133 new Point(m_drawRect.Left + m_lineWidth+2, m_drawRect.Top+m_lineWidth), 134 new Point(m_drawRect.Left + 2, m_drawRect.Top) 135 }); 136 path.CloseAllFigures(); 137 e.Graphics.FillPath(new SolidBrush(ForeColor), path); 138 } 139 140 if (vs.Contains(2)) 141 { 142 GraphicsPath path = new GraphicsPath(); 143 path.AddLines(new Point[] 144 { 145 new Point(m_drawRect.Righ