前提 入行已經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
用處及效果
準備工作
沒什麼可準備的,直接往下看吧
開始
添加一個類UCPanelQuote繼承 Panel
添加2個屬性
1 /// <summary> 2 /// The border color 3 /// </summary> 4 private Color borderColor = LineColors.Light; 5 6 /// <summary> 7 /// Gets or sets the color of the border. 8 /// </summary> 9 /// <value>The color of the border.</value> 10 [Description("邊框顏色"), Category("自定義")] 11 public Color BorderColor 12 { 13 get { return borderColor; } 14 set 15 { 16 borderColor = value; 17 this.Invalidate(); 18 } 19 } 20 21 /// <summary> 22 /// The left color 23 /// </summary> 24 private Color leftColor = StatusColors.Danger; 25 26 /// <summary> 27 /// Gets or sets the color of the left. 28 /// </summary> 29 /// <value>The color of the left.</value> 30 [Description("左側顏色"), Category("自定義")] 31 public Color LeftColor 32 { 33 get { return leftColor; } 34 set 35 { 36 leftColor = value; 37 this.Invalidate(); 38 } 39 }
為了畫邊框和左邊的顏色,設置一下Padding
1 public UCPanelQuote() 2 : base() 3 { 4 Padding = new Padding(5, 1, 1, 1); 5 }
重繪
1 protected override void OnPaint(PaintEventArgs e) 2 { 3 base.OnPaint(e); 4 e.Graphics.SetGDIHigh(); 5 6 e.Graphics.DrawLines(new Pen(borderColor), new Point[] 7 { 8 new Point(e.ClipRectangle.Left,e.ClipRectangle.Top), 9 new Point(e.ClipRectangle.Right-1,e.ClipRectangle.Top), 10 new Point(e.ClipRectangle.Right-1,e.ClipRectangle.Bottom-1), 11 new Point(e.ClipRectangle.Left,e.ClipRectangle.Bottom-1), 12 new Point(e.ClipRectangle.Left,e.ClipRectangle.Top) 13 }); 14 15 e.Graphics.FillRectangle(new SolidBrush(leftColor), new Rectangle(0, 0, 5, this.Height)); 16 }
最後的話
如果你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星星吧